设置帖子的条款。
原型
wp_set_post_terms( int $post_id, string|array $tags = '', string $taxonomy = 'post_tag', bool $append = false )
参数
$post_id
(int)
(Optional)
邮政编码。不默认为全局$ post的ID。
$tags
(string|array)
(Optional)
要为帖子设置的术语数组,或用逗号分隔的一串术语。
$taxonomy
(string)
(Optional)
分类名称。
$append
(bool)
(Optional)
如果为true,请不要删除现有术语,只需添加即可。如果为false,请将条款替换为新条款。
返回值
(array|false|WP_Error)
受影响的术语的术语分类ID的数组。
源文件
路径:wp-includes/post.php
<?php
...
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) {
$post_id = (int) $post_id;
if ( !$post_id )
return false;
if ( empty($tags) )
$tags = array();
if ( ! is_array( $tags ) ) {
$comma = _x( ',', 'tag delimiter' );
if ( ',' !== $comma )
$tags = str_replace( $comma, ',', $tags );
$tags = explode( ',', trim( $tags, " ntr x0B," ) );
}
/*
* Hierarchical taxonomies must always pass IDs rather than names so that
* children with the same names but different parents aren't confused.
*/
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
$tags = array_unique( array_map( 'intval', $tags ) );
}
return wp_set_object_terms( $post_id, $tags, $taxonomy, $append );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_set_post_terms/