wp_set_post_terms

函数
wp_set_post_terms ( $post_id = 0, $terms = '', $taxonomy = 'post_tag', $append = false )
参数
  • (int) $post_id Optional. The Post ID. Does not default to the ID of the global $post.
    Required:
  • (string|array) $terms Optional. An array of terms to set for the post, or a string of terms separated by commas. Hierarchical taxonomies must always pass IDs rather than names so that children with the same names but different parents aren't confused. Default empty.
    Required:
    Default: (empty)
  • (string) $taxonomy Optional. Taxonomy name. Default 'post_tag'.
    Required:
    Default: 'post_tag'
  • (bool) $append Optional. If true, don't delete existing terms, just add on. If false, replace the terms with the new terms. Default false.
    Required:
    Default: false
返回值
  • (array|false|WP_Error) Array of term taxonomy IDs of affected terms. WP_Error or false on failure.
相关
  • wp_set_object_terms()
定义位置
相关方法
wp_get_post_termswp_set_post_tagswp_set_post_catswp_set_object_termswp_set_post_categories
引入
2.8.0
弃用
-

wp_set_post_terms: 这个函数设置与文章相关的术语。它接受文章的ID,术语ID或名称的数组,术语的分类法,以及更新与文章相关的现有术语的各种选项。

设置一个文章的条款。

function wp_set_post_terms( $post_id = 0, $terms = '', $taxonomy = 'post_tag', $append = false ) {
	$post_id = (int) $post_id;

	if ( ! $post_id ) {
		return false;
	}

	if ( empty( $terms ) ) {
		$terms = array();
	}

	if ( ! is_array( $terms ) ) {
		$comma = _x( ',', 'tag delimiter' );
		if ( ',' !== $comma ) {
			$terms = str_replace( $comma, ',', $terms );
		}
		$terms = explode( ',', trim( $terms, " ntrx0B," ) );
	}

	/*
	 * 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 ) ) {
		$terms = array_unique( array_map( 'intval', $terms ) );
	}

	return wp_set_object_terms( $post_id, $terms, $taxonomy, $append );
}

常见问题

FAQs
查看更多 >