wppaste
WordPress

get_object_term_cache( int $id, string $taxonomy ): bool|WP_Term[]|WP_Error

Since
2.3.0, 4.7.0
Source
wp-includes/taxonomy.php:3771
Retrieves the cached term objects for the given object ID.

Description

Upstream functions (like get_the_terms() and is_object_in_term()) are responsible for populating the object-term relationship cache. The current function only fetches relationship data that is already in the cache.

Compatibility

WordPress
since 4.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

$idint
Term object ID, for example a post, comment, or user ID.
$taxonomystring
Taxonomy name.

Return value

bool|WP_Term[]|WP_Error
Array of WP_Term objects, if cached.
False if cache is empty for $taxonomy and $id.
WP_Error if get_term() returns an error object for any term.

Performance profile

How much work a call to get_object_term_cache() 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 get_term().

Scaling
Scales with input

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

Instructions
11–31

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
7

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

What it touches

  • cacheobject cachewp_cache_get()called directly
  • querycontent queryget_term()called directly
  • hookthird-party callbacksapply_filters()one call below get_object_term_cache()

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 · 3 distinct outcomes

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

WhenInstructionsCalls it makes
$_term_ids === false11wp_cache_get()
$_term_ids !== false20–22wp_cache_get(), _prime_term_caches()
$_term_ids !== false && !$term_ids && is_wp_error()30–31wp_cache_get(), _prime_term_caches(), get_term(), is_wp_error()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5011–318
8.55011–318
8.45011–3182 fewer instructions than PHP 8.3
8.35211–318
8.25211–318
8.15211–318
7.45211–318

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 · 4

  • wp_cache_get()Retrieves the cache contents from the cache by key and group.
  • _prime_term_caches()Adds any terms from the given IDs to the cache that do not already exist in cache.
  • get_term()Gets all term data from database by term ID.
  • is_wp_error()Checks whether the given variable is a WordPress Error.

Used by · 7

Source code

function get_object_term_cache( $id, $taxonomy ) {	$_term_ids = wp_cache_get( $id, "{$taxonomy}_relationships" ); 	// We leave the priming of relationship caches to upstream functions.	if ( false === $_term_ids ) {		return false;	} 	// Backward compatibility for if a plugin is putting objects into the cache, rather than IDs.	$term_ids = array();	foreach ( $_term_ids as $term_id ) {		if ( is_numeric( $term_id ) ) {			$term_ids[] = (int) $term_id;		} elseif ( isset( $term_id->term_id ) ) {			$term_ids[] = (int) $term_id->term_id;		}	} 	// Fill the term objects.	_prime_term_caches( $term_ids ); 	$terms = array();	foreach ( $term_ids as $term_id ) {		$term = get_term( $term_id, $taxonomy );		if ( is_wp_error( $term ) ) {			return $term;		} 		$terms[] = $term;	} 	return $terms;}

Changelog

Introduced in 2.3.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.

4.7.0
Returns a WP_Error object if there's an error with any of the matched terms.from the docblock
2.3.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.