get_metadata( string $meta_type, int $object_id, string $meta_key = '', bool $single = false )
Retrieve metadata for the specified object.
描述
参数
- $meta_type
-
(string)
(Required)
Type of object metadata is for (e.g., comment, post, or user) - $object_id
-
(int)
(Required)
ID of the object metadata is for - $meta_key
-
(string)
(Optional)
Metadata key. If not specified, retrieve all metadata for the specified object.Default value: ”
- $single
-
(bool)
(Optional)
If true, return only the first value of the specified meta_key. This parameter has no effect if meta_key is not specified.Default value: false
返回值
(mixed) Single metadata value, or array of values
源代码
File: wp-includes/meta.php
function get_metadata($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;
}
/**
* Filters whether to retrieve metadata of a specific type.
*
* The dynamic portion of the hook, `$meta_type`, refers to the meta
* object type (comment, post, or user). Returning a non-null value
* will effectively short-circuit the function.
*
* @since 3.1.0
*
* @param null|array|string $value The value get_metadata() should return - a single metadata value,
* or an array of values.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param bool $single Whether to return only the first value of the specified $meta_key.
*/
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
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 ) );
$meta_cache = $meta_cache[$object_id];
}
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]);
}
if ($single)
return '';
else
return array();
}
更新日志
Version | 描述 |
---|---|
2.9.0 | Introduced. |
相关函数
Uses
-
wp-includes/cache.php:
wp_cache_get() -
wp-includes/functions.php:
absint() -
wp-includes/functions.php:
maybe_unserialize() -
wp-includes/plugin.php:
apply_filters() -
wp-includes/meta.php:
update_meta_cache() -
wp-includes/meta.php:
get_{$meta_type}_metadata
Show 1 more use
Used By
-
wp-includes/rest-api/fields/class-wp-rest-meta-fields.php:
WP_REST_Meta_Fields::update_multi_meta_value() -
wp-includes/rest-api/fields/class-wp-rest-meta-fields.php:
WP_REST_Meta_Fields::update_meta_value() -
wp-includes/rest-api/fields/class-wp-rest-meta-fields.php:
WP_REST_Meta_Fields::get_value() -
wp-includes/meta.php:
get_registered_metadata() -
wp-includes/taxonomy.php:
get_term_meta() -
wp-includes/user.php:
get_user_meta() -
wp-includes/post.php:
get_post_meta() -
wp-includes/comment.php:
get_comment_meta() -
wp-includes/meta.php:
update_metadata()
Show 4 more used by
Hide more used by
你可能对这些文章感兴趣:
- wordpress函数has_filter()用法示例
- wordpress函数has_meta()用法示例
- wordpress函数has_excerpt()用法示例
- wordpress函数has_header_video()用法示例
- wordpress函数has_category()用法示例
- wordpress函数has_custom_logo()用法示例
- wordpress函数has_custom_header()用法示例
- wordpress函数hash_hmac()用法示例
- wordpress函数grant_super_admin()用法示例
- wordpress函数get_the_post_thumbnail_url()用法示例
- wordpress函数hash_equals()用法示例
- wordpress函数got_url_rewrite()用法示例
- wordpress函数gzip_compression()用法示例
- wordpress函数graceful_fail()用法示例
如有疑问,请前往问答中心反馈!
反馈