返回介绍

get_metadata()

发布于 2017-09-10 23:33:40 字数 4310 浏览 1586 评论 0 收藏 0

get_metadata( string $meta_type,  int $object_id,  string $meta_key = '',  bool $single = false )

Retrieve metadata for the specified object.


description


参数

$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();
}

更新日志

Versiondescription
2.9.0Introduced.

相关函数

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 Hide more uses

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

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文