确定给定对象是否与任何给定术语相关联。

原型

is_object_in_term( int $object_id, string $taxonomy, int|string|array $terms = null )

描述

根据对象的术语’term_ids,名称和slugs检查给定的术语。仅作为整数给出的术语将仅根据对象的术语’term_ids进行检查。如果未给出任何术语,则确定对象是否与给定分类中的任何术语相关联。

参数

$object_id (int) (Required) 对象的ID(帖子ID,链接ID,……)。

$taxonomy (string) (Required) 单一分类名称。

$terms (int|string|array) (Optional)  术语term_id,名称,slug或所述的数组。

返回值

(bool|WP_Error)  

源文件

路径:wp-includes/taxonomy.php

<?php
...
function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
	if ( !$object_id = (int) $object_id )
		return new WP_Error( 'invalid_object', __( 'Invalid object ID.' ) );
	$object_terms = get_object_term_cache( $object_id, $taxonomy );
	if ( false === $object_terms ) {
		$object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) );
		if ( is_wp_error( $object_terms ) ) {
			return $object_terms;
		}
		wp_cache_set( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" );
	}
	if ( is_wp_error( $object_terms ) )
		return $object_terms;
	if ( empty( $object_terms ) )
		return false;
	if ( empty( $terms ) )
		return ( !empty( $object_terms ) );
	$terms = (array) $terms;
	if ( $ints = array_filter( $terms, 'is_int' ) )
		$strs = array_diff( $terms, $ints );
	else
		$strs =& $terms;
	foreach ( $object_terms as $object_term ) {
		// If term is an int, check against term_ids only.
		if ( $ints && in_array( $object_term->term_id, $ints ) ) {
			return true;
		}
		if ( $strs ) {
			// Only check numeric strings against term_id, to avoid false matches due to type juggling.
			$numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) );
			if ( in_array( $object_term->term_id, $numeric_strs, true ) ) {
				return true;
			}
			if ( in_array( $object_term->name, $strs ) ) return true;
			if ( in_array( $object_term->slug, $strs ) ) return true;
		}
	}
	return false;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/is_object_in_term/