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:4898
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
- Scaling
- Scales with input
- Instructions
- 14–86
- Plugin surface
- None
- Called by
- 2
Reaches the database via wp_get_object_terms().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 107.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
wp_get_object_terms()called directly - cacheobject cache
wp_cache_set()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 14 | __() |
$object_terms !== false | 18–22 | get_object_term_cache(), is_wp_error() |
$object_terms === false && is_wp_error() | 24 | get_object_term_cache(), wp_get_object_terms(), is_wp_error() |
$object_terms !== false && !is_wp_error() && !empty($object_terms) && !empty($terms) | 33–39 | get_object_term_cache(), is_wp_error(), array_filter() |
$object_terms !== false && !is_wp_error() && !empty($object_terms) && !empty($terms) | 38–44 | get_object_term_cache(), is_wp_error(), array_filter(), array_diff() |
$object_terms === false && !is_wp_error() | 38–42 | get_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_terms | 49–61 | get_object_term_cache(), is_wp_error(), array_filter(), array_filter(), array_map() |
$object_terms === false && !is_wp_error() && !empty($object_terms) && !empty($terms) | 53–59 | get_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_terms | 54–66 | get_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–64 | get_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_terms | 69–81 | get_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_terms | 74–86 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 107 | 14–86 | 15 | |
| 8.5 | 107 | 14–86 | 15 | |
| 8.4 | 107 | 14–86 | 15 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 119 | 14–98 | 15 | |
| 8.2 | 119 | 14–98 | 15 | |
| 8.1 | 119 | 14–98 | 15 | |
| 7.4 | 119 | 14–98 | 15 |
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
- __()Retrieves the translation of $text.
- get_object_term_cache()Retrieves the cached term objects for the given object ID.
- wp_get_object_terms()Retrieves the terms associated with the given object(s), in the supplied taxonomies.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_cache_set()Saves the data to the cache.
- wp_list_pluck()Plucks a certain field out of each object or array in an array.
- WP_Error::__construct()Initializes the error.
Used by · 2
- has_term()Checks if the current post has any of given terms.
- wp_update_nav_menu_item()Saves the properties of a menu item or create a new one.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.