update_meta_cache
Update the metadata cache for the specified objects.
Description
Returns (array|false)
Metadata cache for the specified objects, or false on failure.
Parameters (2)
- 0. $meta_type (string)
- Type of object metadata is for (e.g., comment, post, or user)
- 1. $object_ids (int|array)
- Array or comma delimited list of object IDs to update cache for
Usage
if ( !function_exists( 'update_meta_cache' ) ) { require_once ABSPATH . WPINC . '/meta.php'; } // Type of object metadata is for (e.g., comment, post, or user) $meta_type = ''; // Array or comma delimited list of object IDs to update cache for $object_ids = null; // NOTICE! Understand what this does before running. $result = update_meta_cache($meta_type, $object_ids);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/meta.php
- 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);
- $cache_key = $meta_type . '_meta';
- $ids = array();
- $cache = array();
- foreach ( $object_ids as $id ) {
- $cached_object = wp_cache_get( $id, $cache_key );
- if ( false === $cached_object )
- $ids[] = $id;
- else
- $cache[$id] = $cached_object;
- }
- if ( empty( $ids ) )
- return $cache;
- // Get meta info
- $id_list = join( ', ', $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 = intval($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();
- // Add a value to the current pid/key:
- $cache[$mpid][$mkey][] = $mval;
- }
- }
- foreach ( $ids as $id ) {
- if ( ! isset($cache[$id]) )
- $cache[$id] = array();
- wp_cache_add( $id, $cache[$id], $cache_key );
- }
- return $cache;
- }