返回介绍

wp_set_post_terms()

发布于 2017-09-11 12:54:31 字数 4493 浏览 1250 评论 0 收藏 0

wp_set_post_terms( int $post_id,  string|array $tags = '',  string $taxonomy = 'post_tag',  bool $append = false )

Set the terms for a post.


description


参数

$post_id

(int) (Optional) The Post ID. Does not default to the ID of the global $post.

$tags

(string|array) (Optional) An array of terms to set for the post, or a string of terms separated by commas.

Default value: ''

$taxonomy

(string) (Optional) Taxonomy name.

Default value: 'post_tag'

$append

(bool) (Optional) If true, don't delete existing terms, just add on. If false, replace the terms with the new terms.

Default value: false


返回值

(array|false|WP_Error) Array of term taxonomy IDs of affected terms. WP_Error or false on failure.


源代码

File: wp-includes/post.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, " \n\t\r\0\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 );
}

更新日志

Versiondescription
2.8.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: _x()
  • wp-includes/taxonomy.php: wp_set_object_terms()
  • wp-includes/taxonomy.php: is_taxonomy_hierarchical()

Used By

  • wp-includes/post.php: wp_set_post_tags()
  • wp-includes/post.php: wp_set_post_categories()
  • wp-includes/post.php: wp_insert_post()
  • wp-includes/post-formats.php: set_post_format()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note Contributed by Codex

    Non hierarchical term example

    For non-hierarchical terms (such as tags), you can pass either the term name or id. If you pass the id there is only one caveat: You must pass it as an integer, and it must be in an array. This is necessary because any non-array value passed will be converted to a string, which will be interpreted as a term name.

    
    $tag = '5'; // Wrong. This will add the tag with the *name* '5'.
    $tag = 5; // Wrong. This will also add the tag with the name '5'.
    $tag = array( '5' ); // Wrong. Again, this will be interpreted as a term name rather than an id.
    
    $tag = array( 5 ); // Correct. This will add the tag with the id 5.
    wp_set_post_terms( $post_id, $tag, $taxonomy );
    

    This function will only work on the native post type. For a taxonomy on a custom post type use wp_set_object_terms()

  2. Hierarchical term example

    For hierarchical terms (such as categories), you must always pass the id rather than the term name to avoid confusion where there may be another child with the same name.

    To get the term id you can use:

    
    $term_id = term_exists( $term, $taxonomy, $parent );
    

    You may also need to pass by reference:

    
    wp_set_post_terms( $post_id, $term, &$taxonomy );
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文