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:3748
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_Termobjects, if cached.
False if cache is empty for$taxonomyand$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
- Scaling
- Scales with input
- Instructions
- 11–31
- Plugin surface
- None
- Called by
- 7
Reaches the database via get_term().
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 50.
Nothing here hands control to plugin code.
7 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_get()called directly - querycontent query
get_term()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
$_term_ids === false | 11 | wp_cache_get() |
$_term_ids !== false | 20–22 | wp_cache_get(), _prime_term_caches() |
$_term_ids !== false && !$term_ids && is_wp_error() | 30–31 | wp_cache_get(), _prime_term_caches(), get_term(), is_wp_error() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 50 | 11–31 | 8 | |
| 8.5 | 50 | 11–31 | 8 | |
| 8.4 | 50 | 11–31 | 8 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 52 | 11–31 | 8 | |
| 8.2 | 52 | 11–31 | 8 | |
| 8.1 | 52 | 11–31 | 8 | |
| 7.4 | 52 | 11–31 | 8 |
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
- get_attachment_fields_to_edit()Retrieves the attachment fields to edit form fields.
- get_compat_media_markup()
- get_inline_data()Adds hidden fields with the data for use in the inline editor for posts and pages.
- get_terms_to_edit()Gets comma-separated list of terms available to edit for the given post ID.
- get_the_taxonomies()Retrieves all taxonomies associated with a post.
- get_the_terms()Retrieves the terms of the taxonomy that are attached to the post.
- is_object_in_term()Determines if the given object is associated with any of the given terms.
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.
Signature, return type and hooks compared across 5 parsed releases.
WP_Error object if there's an error with any of the matched terms.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.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.