WP_Term::get_instance( int $term_id, string $taxonomy = null ): WP_Term|WP_Error|false
- Since
- 4.4.0
- Source
wp-includes/class-wp-term.php:116
Compatibility
- WordPress
- since 4.4.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
$term_idint- Term ID.
$taxonomystringoptional- Limit matched terms to those matching
$taxonomy. Only used for disambiguating potentially shared terms.Default:null
Return value
WP_Term|WP_Error|false- Term object, if found. WP_Error if
$term_idis shared between taxonomies and there's insufficient data to distinguish which term is intended.
False for other failures.
Performance profile
How much work a call to WP_Term::get_instance() 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
- 7–75
- Plugin surface
- None
- Called by
- 1
Reaches the database via ->get_results().
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 116.
Nothing here hands control to plugin code.
1 place 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 - sqldatabase query
->get_results()called directly
Further down the call graph this can also reach hook, option, serialize, query 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 · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Term::get_instance() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7 | none |
| always | 23–26 | wp_cache_get(), ->filter() |
| always | 30–44 | wp_cache_get(), ->prepare(), ->get_results() |
| always | 40–44 | wp_cache_get(), ->prepare(), ->get_results(), reset() |
| always | 48–57 | wp_cache_get(), ->prepare(), ->get_results(), taxonomy_exists(), __() |
!taxonomy_exists() | 53–57 | wp_cache_get(), ->prepare(), ->get_results(), reset(), taxonomy_exists(), __() |
taxonomy_exists() | 61–70 | wp_cache_get(), ->prepare(), ->get_results(), taxonomy_exists(), sanitize_term(), ->filter() |
taxonomy_exists() | 66–75 | wp_cache_get(), ->prepare(), ->get_results(), taxonomy_exists(), sanitize_term(), wp_cache_add(), ->filter() |
taxonomy_exists() | 66–70 | wp_cache_get(), ->prepare(), ->get_results(), reset(), taxonomy_exists(), sanitize_term(), ->filter() |
taxonomy_exists() | 71–75 | wp_cache_get(), ->prepare(), ->get_results(), reset(), taxonomy_exists(), sanitize_term(), wp_cache_add(), ->filter() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 116 instructions, 7–75 executed per call, 17 branches. The work does not change between versions.
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
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- taxonomy_exists()Determines whether the taxonomy name exists.
- __()Retrieves the translation of $text.
- sanitize_term()Sanitizes all term fields.
- wp_cache_add()Adds data to the cache, if the cache key doesn't already exist.
- WP_Error::__construct()Initializes the error.
- WP_Term::__construct()Constructor.
Used by · 1
- get_term()Gets all term data from database by term ID.
Source code
public static function get_instance( $term_id, $taxonomy = null ) { global $wpdb; $term_id = (int) $term_id; if ( ! $term_id ) { return false; } $_term = wp_cache_get( $term_id, 'terms' ); // If there isn't a cached version, hit the database. if ( ! $_term || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) { // Any term found in the cache is not a match, so don't use it. $_term = false; // Grab all matching terms, in case any are shared between taxonomies. $terms = $wpdb->get_results( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id = %d", $term_id ) ); if ( ! $terms ) { return false; } // If a taxonomy was specified, find a match. if ( $taxonomy ) { foreach ( $terms as $match ) { if ( $taxonomy === $match->taxonomy ) { $_term = $match; break; } } // If only one match was found, it's the one we want. } elseif ( 1 === count( $terms ) ) { $_term = reset( $terms ); // Otherwise, the term must be shared between taxonomies. } else { // If the term is shared only with invalid taxonomies, return the one valid term. foreach ( $terms as $t ) { if ( ! taxonomy_exists( $t->taxonomy ) ) { continue; } // Only hit if we've already identified a term in a valid taxonomy. if ( $_term ) { return new WP_Error( 'ambiguous_term_id', __( 'Term ID is shared between multiple taxonomies' ), $term_id ); } $_term = $t; } } if ( ! $_term ) { return false; } // Don't return terms from invalid taxonomies. if ( ! taxonomy_exists( $_term->taxonomy ) ) { return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); } $_term = sanitize_term( $_term, $_term->taxonomy, 'raw' ); // Don't cache terms that are shared between taxonomies. if ( 1 === count( $terms ) ) { wp_cache_add( $term_id, $_term, 'terms' ); } } $term_obj = new WP_Term( $_term ); $term_obj->filter( $term_obj->filter ); return $term_obj; }Changelog
Introduced in 4.4.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.8.8 tag, from
src/wp-includes/class-wp-term.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.