wppaste
WordPress

is_object_in_term( int $object_id, string $taxonomy, int|string|int[]|string[] $terms = null ): bool|WP_Error

Since
2.7.0
Source
wp-includes/taxonomy.php:4944
Determines if the given object is associated with any of the given terms.

Description

The given terms are checked against the object's terms' term_ids, names and slugs.
Terms given as integers will only be checked against the object's terms' term_ids.
If no terms are given, determines if object is associated with any terms in the given taxonomy.

Compatibility

WordPress
since 2.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$object_idint
ID of the object (post ID, link ID, ...).
$taxonomystring
Single taxonomy name.
$termsint|string|int[]|string[]optional
Term ID, name, slug, or array of such to check against. Default null.Default: null

Return value

bool|WP_Error
WP_Error on input error.

Performance profile

How much work a call to is_object_in_term() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via wp_get_object_terms().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
14–86

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 107.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent querywp_get_object_terms()called directly
  • cacheobject cachewp_cache_set()called directly
  • hookthird-party callbacksapply_filters()one call below is_object_in_term()

Further down the call graph this can also reach option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 12 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost is_object_in_term() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always14__()
$object_terms !== false18–22get_object_term_cache(), is_wp_error()
$object_terms === false && is_wp_error()24get_object_term_cache(), wp_get_object_terms(), is_wp_error()
$object_terms !== false && !is_wp_error() && !empty($object_terms) && !empty($terms)33–39get_object_term_cache(), is_wp_error(), array_filter()
$object_terms !== false && !is_wp_error() && !empty($object_terms) && !empty($terms)38–44get_object_term_cache(), is_wp_error(), array_filter(), array_diff()
$object_terms === false && !is_wp_error()38–42get_object_term_cache(), wp_get_object_terms(), is_wp_error(), wp_list_pluck(), wp_cache_set(), is_wp_error()
$object_terms !== false && !is_wp_error() && !empty($object_terms) && !empty($terms) && !$object_terms49–61get_object_term_cache(), is_wp_error(), array_filter(), array_filter(), array_map()
$object_terms === false && !is_wp_error() && !empty($object_terms) && !empty($terms)53–59get_object_term_cache(), wp_get_object_terms(), is_wp_error(), wp_list_pluck(), wp_cache_set(), is_wp_error(), array_filter()
$object_terms !== false && !is_wp_error() && !empty($object_terms) && !empty($terms) && !$object_terms54–66get_object_term_cache(), is_wp_error(), array_filter(), array_diff(), array_filter(), array_map()
$object_terms === false && !is_wp_error() && !empty($object_terms) && !empty($terms)58–64get_object_term_cache(), wp_get_object_terms(), is_wp_error(), wp_list_pluck(), wp_cache_set(), is_wp_error(), array_filter(), array_diff()
$object_terms === false && !is_wp_error() && !empty($object_terms) && !empty($terms) && !$object_terms69–81get_object_term_cache(), wp_get_object_terms(), is_wp_error(), wp_list_pluck(), wp_cache_set(), is_wp_error(), array_filter(), array_filter(), array_map()
$object_terms === false && !is_wp_error() && !empty($object_terms) && !empty($terms) && !$object_terms74–86get_object_term_cache(), wp_get_object_terms(), is_wp_error(), wp_list_pluck(), wp_cache_set(), is_wp_error(), array_filter(), array_diff(), array_filter(), array_map()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10714–8615
8.510714–8615
8.410714–861512 fewer instructions than PHP 8.3
8.311914–9815
8.211914–9815
8.111914–9815
7.411914–9815

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 7

Used by · 2

Source code

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;}

Changelog

Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/taxonomy.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.