get_metadata_raw()
云策文档标注
概述
get_metadata_raw() 函数用于检索指定对象的原始元数据值,支持多种对象类型如 'blog'、'post'、'comment'、'term'、'user' 等。它通过参数控制返回单个值或数组,并包含缓存机制和钩子以优化性能。
关键要点
- 参数:$meta_type(必需,对象类型)、$object_id(必需,对象ID)、$meta_key(可选,元数据键)、$single(可选,是否返回单个值)。
- 返回值:根据 $single 参数返回数组或单个值;无效输入返回 false;不存在返回 null。
- 内部机制:使用 wp_cache_get() 和 update_meta_cache() 进行缓存管理,提高效率。
- 钩子:通过 apply_filters( "get_{$meta_type}_metadata" ) 支持动态短路返回值。
- 相关函数:与 get_metadata()、update_metadata() 等协同工作,用于元数据操作。
代码示例
// 示例:检索文章的元数据
$post_meta = get_metadata_raw('post', 123, 'custom_field', true);
if ($post_meta !== null) {
echo $post_meta; // 输出单个值
}注意事项
- 确保 $object_id 为正整数,否则函数返回 false。
- 当 $meta_key 未指定时,返回该对象的所有元数据缓存数组。
- 返回值可能经过 maybe_unserialize() 处理,以反序列化存储的数据。
原文内容
Retrieves raw metadata value for the specified object.
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_idintrequired-
ID of the object metadata is for.
$meta_keystringoptional-
Metadata key. If not specified, retrieve all metadata for the specified object. Default empty string.
$singlebooloptional-
If true, return only the first value of the specified
$meta_key.
This parameter has no effect if$meta_keyis not specified.Default:
false
Source
function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) {
if ( ! $meta_type || ! is_numeric( $object_id ) ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
/**
* Short-circuits the return value of a meta field.
*
* 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 filter names include:
*
* - `get_blog_metadata`
* - `get_post_metadata`
* - `get_comment_metadata`
* - `get_term_metadata`
* - `get_user_metadata`
*
* @since 3.1.0
* @since 5.5.0 Added the `$meta_type` parameter.
*
* @param mixed $value The value to return, either a single metadata value or an array
* of values depending on the value of `$single`. Default null.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param bool $single Whether to return only the first value of the specified `$meta_key`.
* @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
* 'user', or any other object type with an associated meta table.
*/
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single, $meta_type );
if ( null !== $check ) {
if ( $single && is_array( $check ) ) {
return $check[0];
} else {
return $check;
}
}
$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
if ( ! $meta_cache ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
if ( isset( $meta_cache[ $object_id ] ) ) {
$meta_cache = $meta_cache[ $object_id ];
} else {
$meta_cache = null;
}
}
if ( ! $meta_key ) {
return $meta_cache;
}
if ( isset( $meta_cache[ $meta_key ] ) ) {
if ( $single ) {
return maybe_unserialize( $meta_cache[ $meta_key ][0] );
} else {
return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] );
}
}
return null;
}
Hooks
- apply_filters( “get_{$meta_type}_metadata”, mixed $value, int $object_id, string $meta_key, bool $single, string $meta_type )
-
Short-circuits the return value of a meta field.
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Introduced. |