update_meta_cache()
云策文档标注
概述
update_meta_cache() 函数用于更新指定对象的元数据缓存,以提高性能并减少数据库查询。它支持多种对象类型,如 'blog'、'post'、'comment'、'term'、'user' 等,并返回缓存数据或失败时返回 false。
关键要点
- 参数 $meta_type 指定对象类型,必须是字符串,如 'post' 或 'user'。
- 参数 $object_ids 可以是数组或逗号分隔的字符串,表示要更新缓存的对象 ID。
- 函数内部使用 wp_cache_get_multiple() 和 wp_cache_add_multiple() 来管理缓存。
- 包含一个动态 Hook:apply_filters( "update_{$meta_type}_metadata_cache", ... ),允许短路径更新。
- 返回类型为数组(缓存数据)或 false(失败时)。
代码示例
// 示例:更新 post 类型对象的元数据缓存
$object_ids = array(1, 2, 3);
$meta_cache = update_meta_cache('post', $object_ids);
if ($meta_cache !== false) {
// 处理缓存数据
print_r($meta_cache);
}注意事项
- 确保 $meta_type 有效,否则函数可能返回 false。
- 对象 ID 会被转换为整数数组,非数字字符将被过滤。
- Hook 可用于自定义缓存更新逻辑,返回非 null 值将短路径函数执行。
原文内容
Updates the metadata cache for the specified objects.
Parameters
$meta_typestringrequired-
Type of object metadata is for. Accepts
'blog','post','comment','term','user', or any other object type with an associated meta table. $object_idsstring|int[]required-
Array or comma delimited list of object IDs to update cache for.
Source
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
* (blog, 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_blog_metadata_cache`
* - `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();
}
// Add a value to the current pid/key.
$cache[ $mpid ][ $mkey ][] = $mval;
}
}
$data = array();
foreach ( $non_cached_ids as $id ) {
if ( ! isset( $cache[ $id ] ) ) {
$cache[ $id ] = array();
}
$data[ $id ] = $cache[ $id ];
}
wp_cache_add_multiple( $data, $cache_group );
return $cache;
}
Hooks
- apply_filters( “update_{$meta_type}_metadata_cache”, mixed $check, int[] $object_ids )
-
Short-circuits updating the metadata cache of a specific type.
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |