is_object_in_term()WP 2.7.0

Defines whether the specified object is associated with any of the specified taxonomy elements (term). Specific term(s) can be provided to check the association.

The specified terms are compared with the ID, name, and slug of the object's terms. If terms are passed as integers, the comparison will only be with the object's term IDs. If no term is specified, the function will check if the object has at least one term from the specified taxonomy.

Used By: has_term()
1 time — 0.000809 sec (slow) | 50000 times — 1.16 sec (fast) | PHP 7.0.8, WP 4.6.1

No Hooks.

Returns

true|false|WP_Error. true if the object belongs to the term or false. WP_Error object if an error occurred.

Usage

if( is_object_in_term( $object_id, $taxonomy, $terms ) ){
	...
}
$object_id(integer) (required)
ID of the post whose association with the term needs to be checked.
$taxonomy(string) (required)
Name of the taxonomy to which the term from the $terms parameter belongs.
$terms(string/array/integer)
ID, name, or slug of the term. Multiple terms can be specified in an array.
Default: null

Examples

0

#1 Checking if there are terms for the object (post)

Let's check if the current post ($post->ID) is in the term 'Languages' (slug=langs or ID=5), taxonomy 'my_taxonomy':

$is_in = is_object_in_term( $post->ID, 'my_taxonomy', 'langs' );

if ( $is_in ) {
	echo 'The post is in the term Languages';
}

// it is also possible to specify the term name (not slug)
$is_in = is_object_in_term( $post->ID, 'my_taxonomy', 'Languages' );

// or ID
$is_in = is_object_in_term( $post->ID, 'my_taxonomy', 5 );
0

#2 Multiple term check

$is_in = is_object_in_term( $post->ID, 'my_taxonomy', [ 'Languages', 25 ] );

if( $is_in ){
	echo 'The post is related to one of the terms: Languages', 25';
}

Changelog

Since 2.7.0 Introduced.

is_object_in_term() code WP 7.0

function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
	$object_id = (int) $object_id;
	if ( ! $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 true;
	}

	$terms = (array) $terms;

	$ints = array_filter( $terms, 'is_int' );
	if ( $ints ) {
		$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, true ) ) {
			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, true ) ) {
				return true;
			}
			if ( in_array( $object_term->slug, $strs, true ) ) {
				return true;
			}
		}
	}

	return false;
}