wppaste
WordPress

update_object_term_cache( string|int[] $object_ids, string|string[] $object_type ): void|false

Since
2.3.0
Source
wp-includes/taxonomy.php:3864
Updates the cache for the given term object ID(s).

Description

Note: Due to performance concerns, great care should be taken to only update term caches when necessary. Processing time can increase exponentially depending on both the number of passed term IDs and the number of taxonomies those terms belong to.

Caches will only be updated for terms not already cached.

Compatibility

WordPress
since 2.3.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_idsstring|int[]
Comma-separated list or array of term object IDs.
$object_typestring|string[]
The taxonomy object type or array of the same.

Return value

void|false
Void on success or if the $object_ids parameter is empty, false if all of the terms in $object_ids are already cached.

Performance profile

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

Scaling
Scales with input

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

Instructions
5–52

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

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

  • cacheobject cachewp_cache_get_multiple()called directly
  • querycontent querywp_get_object_terms()called directly
  • hookthird-party callbacksapply_filters()one call below update_object_term_cache()

Further down the call graph this can also reach serialize, option 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 · 5 distinct outcomes

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

WhenInstructionsCalls it makes
empty($object_ids)5none
!empty($object_ids) && is_array($object_ids) && empty($non_cached_ids)21–22array_map(), get_object_taxonomies()
!empty($object_ids) && !is_array($object_ids) && empty($non_cached_ids)26–27explode(), array_map(), get_object_taxonomies()
!empty($object_ids) && is_array($object_ids) && !empty($non_cached_ids)42–47array_map(), get_object_taxonomies(), array_unique(), wp_get_object_terms()
!empty($object_ids) && !is_array($object_ids) && !empty($non_cached_ids)47–52explode(), array_map(), get_object_taxonomies(), array_unique(), wp_get_object_terms()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 111 instructions, 5–52 executed per call, 22 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 · 4

  • get_object_taxonomies()Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.
  • wp_cache_get_multiple()Retrieves multiple values from the cache in one call.
  • wp_get_object_terms()Retrieves the terms associated with the given object(s), in the supplied taxonomies.
  • wp_cache_add_multiple()Adds multiple values to the cache in one call, if the cache keys don't already exist.

Used by · 2

Source code

function update_object_term_cache( $object_ids, $object_type ) {	if ( empty( $object_ids ) ) {		return;	} 	if ( ! is_array( $object_ids ) ) {		$object_ids = explode( ',', $object_ids );	} 	$object_ids     = array_map( 'intval', $object_ids );	$non_cached_ids = array(); 	$taxonomies = get_object_taxonomies( $object_type ); 	foreach ( $taxonomies as $taxonomy ) {		$cache_values = wp_cache_get_multiple( (array) $object_ids, "{$taxonomy}_relationships" ); 		foreach ( $cache_values as $id => $value ) {			if ( false === $value ) {				$non_cached_ids[] = $id;			}		}	} 	if ( empty( $non_cached_ids ) ) {		return false;	} 	$non_cached_ids = array_unique( $non_cached_ids ); 	$terms = wp_get_object_terms(		$non_cached_ids,		$taxonomies,		array(			'fields'                 => 'all_with_object_id',			'orderby'                => 'name',			'update_term_meta_cache' => false,		)	); 	$object_terms = array();	foreach ( (array) $terms as $term ) {		$object_terms[ $term->object_id ][ $term->taxonomy ][] = $term->term_id;	} 	foreach ( $non_cached_ids as $id ) {		foreach ( $taxonomies as $taxonomy ) {			if ( ! isset( $object_terms[ $id ][ $taxonomy ] ) ) {				if ( ! isset( $object_terms[ $id ] ) ) {					$object_terms[ $id ] = array();				}				$object_terms[ $id ][ $taxonomy ] = array();			}		}	} 	$cache_values = array();	foreach ( $object_terms as $id => $value ) {		foreach ( $value as $taxonomy => $terms ) {			$cache_values[ $taxonomy ][ $id ] = $terms;		}	}	foreach ( $cache_values as $taxonomy => $data ) {		wp_cache_add_multiple( $data, "{$taxonomy}_relationships" );	}}

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.

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.