wppaste
WordPress

update_meta_cache( string $meta_type, string|int[] $object_ids ): array|false

Since
2.9.0
Source
wp-includes/meta.php:1123
Updates the metadata cache for the specified objects.

Compatibility

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

$meta_typestring
Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', or any other object type with an associated meta table.
$object_idsstring|int[]
Array or comma delimited list of object IDs to update cache for.

Return value

array|false
Metadata cache for the specified objects, or false on failure.

Performance profile

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

Scaling
Scales with input

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

Instructions
5–91

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

Plugin surface
1 hook

Third-party callbacks on 'update_{$meta_type}_metadata_cache' run inside this call, and their cost is not bounded by anything here.

Called by
9

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

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get_multiple()called directly
  • sqldatabase query->get_results()called directly

What one call costs · 8 distinct outcomes

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

WhenInstructionsCalls it makes
always5–6none
always11_get_meta_table()
is_array($object_ids) && $check !== null35_get_meta_table(), sanitize_key(), array_map(), apply_filters()
!is_array($object_ids) && $check !== null43_get_meta_table(), sanitize_key(), explode(), array_map(), apply_filters()
is_array($object_ids) && $check === null && empty($non_cached_ids)46–47_get_meta_table(), sanitize_key(), array_map(), apply_filters(), wp_cache_get_multiple()
!is_array($object_ids) && $check === null && empty($non_cached_ids)54–55_get_meta_table(), sanitize_key(), explode(), array_map(), apply_filters(), wp_cache_get_multiple()
is_array($object_ids) && $check === null && !empty($non_cached_ids)77–83_get_meta_table(), sanitize_key(), array_map(), apply_filters(), wp_cache_get_multiple(), ->get_results(), wp_cache_add_multiple()
!is_array($object_ids) && $check === null && !empty($non_cached_ids)85–91_get_meta_table(), sanitize_key(), explode(), array_map(), apply_filters(), wp_cache_get_multiple(), ->get_results(), wp_cache_add_multiple()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1425–9120
8.51425–9120
8.41425–91207 fewer instructions than PHP 8.3
8.31495–9820
8.21495–9820
8.11495–9820
7.41495–9820

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.

Hooks and filters fired · 1

One hook fires while update_meta_cache() runs, in this order:

  1. apply_filters( update_{$meta_type}_metadata_cache )filterline 1163 (+40 into the body)

    Short-circuits updating the metadata cache of a specific type.

Uses · 5

Used by · 9

Source code

function update_meta_cache( $meta_type, $object_ids ) {	global $wpdb; 	if ( ! $meta_type || ! $object_ids ) {		return false;	} 	$table = _get_meta_table( $meta_type );	if ( ! $table ) {		return false;	} 	$column = sanitize_key( $meta_type . '_id' ); 	if ( ! is_array( $object_ids ) ) {		$object_ids = preg_replace( '|[^0-9,]|', '', $object_ids );		$object_ids = explode( ',', $object_ids );	} 	$object_ids = array_map( 'intval', $object_ids ); 	/**	 * Short-circuits updating the metadata cache of a specific type.	 *	 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type	 * (post, comment, term, user, or any other type with an associated meta table).	 * Returning a non-null value will effectively short-circuit the function.	 *	 * Possible hook names include:	 *	 *  - `update_post_metadata_cache`	 *  - `update_comment_metadata_cache`	 *  - `update_term_metadata_cache`	 *  - `update_user_metadata_cache`	 *	 * @since 5.0.0	 *	 * @param mixed $check      Whether to allow updating the meta cache of the given type.	 * @param int[] $object_ids Array of object IDs to update the meta cache for.	 */	$check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids );	if ( null !== $check ) {		return (bool) $check;	} 	$cache_group    = $meta_type . '_meta';	$non_cached_ids = array();	$cache          = array();	$cache_values   = wp_cache_get_multiple( $object_ids, $cache_group ); 	foreach ( $cache_values as $id => $cached_object ) {		if ( false === $cached_object ) {			$non_cached_ids[] = $id;		} else {			$cache[ $id ] = $cached_object;		}	} 	if ( empty( $non_cached_ids ) ) {		return $cache;	} 	// Get meta info.	$id_list   = implode( ',', $non_cached_ids );	$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 	$meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A ); 	if ( ! empty( $meta_list ) ) {		foreach ( $meta_list as $metarow ) {			$mpid = (int) $metarow[ $column ];			$mkey = $metarow['meta_key'];			$mval = $metarow['meta_value']; 			// Force subkeys to be array type.			if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) {				$cache[ $mpid ] = array();			}			if ( ! isset( $cache[ $mpid ][ $mkey ] ) || ! is_array( $cache[ $mpid ][ $mkey ] ) ) {				$cache[ $mpid ][ $mkey ] = array();

Changelog

Introduced in 2.9.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 6.8.8 tag, from src/wp-includes/meta.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.