函数文档

is_object_in_taxonomy()

💡 云策文档标注

概述

is_object_in_taxonomy() 函数用于检查指定的对象类型是否与给定的 taxonomy 相关联。它通过调用 get_object_taxonomies() 获取对象类型的 taxonomy 列表,并验证目标 taxonomy 是否在其中。

关键要点

  • 函数接受两个必需参数:$object_type(对象类型字符串)和 $taxonomy(单个 taxonomy 名称)。
  • 返回布尔值:如果对象类型与 taxonomy 关联则返回 true,否则返回 false。
  • 内部实现基于 get_object_taxonomies() 和 in_array() 函数进行判断。

代码示例

function is_object_in_taxonomy( $object_type, $taxonomy ) {
	$taxonomies = get_object_taxonomies( $object_type );
	if ( empty( $taxonomies ) ) {
		return false;
	}
	return in_array( $taxonomy, $taxonomies, true );
}

注意事项

  • 该函数自 WordPress 3.0.0 版本引入,是核心 taxonomy 功能的一部分。
  • 常用于权限检查、列表过滤和数据处理场景,如 WP_REST_Terms_Controller 和 WP_Posts_List_Table 中。

📄 原文内容

Determines if the given object type is associated with the given taxonomy.

Parameters

$object_typestringrequired
Object type string.
$taxonomystringrequired
Single taxonomy name.

Return

bool True if object is associated with the taxonomy, otherwise false.

Source

function is_object_in_taxonomy( $object_type, $taxonomy ) {
	$taxonomies = get_object_taxonomies( $object_type );
	if ( empty( $taxonomies ) ) {
		return false;
	}
	return in_array( $taxonomy, $taxonomies, true );
}

Changelog

Version Description
3.0.0 Introduced.