检索指定对象的元数据。

原型

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

参数

$meta_type (string) (Required) 对象元数据的类型用于(例如,评论,帖子,术语或用户)。

$object_id (int) (Required) 对象元数据的ID用于

$meta_key (string) (Optional)  元数据键。如果未指定,则检索指定对象的所有元数据。

$single (bool) (Optional)                            如果为true,则仅返回指定meta_key的第一个值。如果未指定meta_key,则此参数无效。

返回值

(mixed)  单个元数据值或值数组

源文件

路径:wp-includes/meta.php

<?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, term, 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();
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/get_metadata/