update_meta_cache( string $meta_type, string|int[] $object_ids ): array|false
- Since
- 2.9.0
- Source
wp-includes/meta.php:1112
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
- Scaling
- Scales with input
- Instructions
- 5–91
- Plugin surface
- 1 hook
- Called by
- 9
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 142.
Third-party callbacks on 'update_{$meta_type}_metadata_cache' run inside this call, and their cost is not bounded by anything here.
9 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5–6 | none |
| always | 11 | _get_meta_table() |
is_array($object_ids) && $check !== null | 35 | _get_meta_table(), sanitize_key(), array_map(), apply_filters() |
!is_array($object_ids) && $check !== null | 43 | _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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 142 | 5–91 | 20 | |
| 8.5 | 142 | 5–91 | 20 | |
| 8.4 | 142 | 5–91 | 20 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 149 | 5–98 | 20 | |
| 8.2 | 149 | 5–98 | 20 | |
| 8.1 | 149 | 5–98 | 20 | |
| 7.4 | 149 | 5–98 | 20 |
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:
- apply_filters( update_{$meta_type}_metadata_cache )filterline 1152 (+40 into the body)
Short-circuits updating the metadata cache of a specific type.
Uses · 5
- _get_meta_table()Retrieves the name of the metadata table for the specified object type.
- sanitize_key()Sanitizes a string key.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_cache_get_multiple()Retrieves multiple values from the cache in one call.
- wp_cache_add_multiple()Adds multiple values to the cache in one call, if the cache keys don't already exist.
Used by · 9
- WP_Metadata_Lazyloader::lazyload_meta_callback()Lazy-loads meta for queued objects.
- cache_users()Retrieves info for user lists to prevent multiple queries by get_userdata().
- get_metadata_raw()Retrieves raw metadata value for the specified object.
- get_user_metavalues()Perform the query to get the $metavalues array(s) needed by _fill_user and _fill_many_users
- metadata_exists()Determines if a meta field with the given key exists for the given object ID.
- update_comment_cache()Updates the comment cache of given comments.
- update_postmeta_cache()Updates metadata cache for a list of post IDs.
- update_sitemeta_cache()Updates metadata cache for list of site IDs.
- update_termmeta_cache()Updates metadata cache for list of term IDs.
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_key = $meta_type . '_meta'; $non_cached_ids = array(); $cache = array(); $cache_values = wp_cache_get_multiple( $object_ids, $cache_key ); 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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.