函数文档

wp_add_object_terms()

💡 云策文档标注

概述

wp_add_object_terms() 函数用于向指定对象添加一个或多个分类术语,是 wp_set_object_terms() 的包装函数,专门用于追加术语而不替换现有术语。

关键要点

  • 参数:$object_id(对象ID,整数,必需),$terms(术语,可以是字符串、整数或数组,必需),$taxonomy(分类法名称,数组或字符串,必需)
  • 返回值:返回受影响术语的分类法ID数组,或 WP_Error 对象
  • 内部实现:调用 wp_set_object_terms() 并设置 $append 参数为 true
  • 相关函数:wp_set_object_terms() 用于创建术语和分类法关系
  • 版本历史:自 WordPress 3.6.0 引入

代码示例

wp_add_object_terms( $attachment_id, 'Uncategorized', 'category' );

📄 原文内容

Adds term(s) associated with a given object.

Parameters

$object_idintrequired
The ID of the object to which the terms will be added.
$termsstring|int|arrayrequired
The slug(s) or ID(s) of the term(s) to add.
$taxonomyarray|stringrequired
Taxonomy name.

Return

array|WP_Error Term taxonomy IDs of the affected terms.

Source

function wp_add_object_terms( $object_id, $terms, $taxonomy ) {
	return wp_set_object_terms( $object_id, $terms, $taxonomy, true );
}

Changelog

Version Description
3.6.0 Introduced.

User Contributed Notes