get_metadata

函数


get_metadata ( $meta_type, $object_id, $meta_key = '', $single = false )
参数
  • (string)
    $meta_type
    Type of object metadata is for. Accepts ‘post’, ‘comment’, ‘term’, ‘user’, or any other object type with an associated meta table.
    Required:
  • (int)
    $object_id
    ID of the object metadata is for.
    Required:
  • (string)
    $meta_key
    Optional. Metadata key. If not specified, retrieve all metadata for the specified object. Default empty.
    Required:
    Default: (empty)
  • (bool)
    $single
    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 false.
    Required:
    Default: false
返回值
  • (mixed) An array of values if `$single` is false. The value of the meta field if `$single` is true. False for an invalid `$object_id` (non-numeric, zero, or negative value), or if `$meta_type` is not specified. An empty string if a valid but non-existing object ID is passed.
相关
  • get_metadata_raw()
  • get_metadata_default()
定义位置
  • wp-includes/meta.php
    , line 570
引入
2.9.0
弃用

检索指定对象类型和ID的元数据字段的值。

如果元数据字段存在,如果`$single`为真,则返回一个单一的值,如果为假,则返回一个数组的值。

如果元字段不存在,其结果取决于get_metadata_default()。默认情况下,如果`$single`为真,将返回一个空字符串,如果为假,将返回一个空数组。

function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
	$value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single );
	if ( ! is_null( $value ) ) {
		return $value;
	}

	return get_metadata_default( $meta_type, $object_id, $meta_key, $single );
}