get_registered_metadata

函数
get_registered_metadata ( $object_type, $object_id, $meta_key = '' )
参数
  • (string) $object_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 the metadata is for.
    Required:
  • (string) $meta_key Optional. Registered metadata key. If not specified, retrieve all registered metadata for the specified object.
    Required:
    Default: (empty)
返回值
  • (mixed) A single value or array of values for a key if specified. An array of all registered keys and values for an object ID if not. False if a given $meta_key is not registered.
定义位置
相关方法
get_registered_meta_keysget_registered_settingsget_registered_theme_featureget_registered_theme_featuresget_registered_nav_menus
引入
4.6.0
弃用
-

get_registered_metadata函数是一个WordPress函数,用于检索特定对象类型的注册元数据,如文章或用户: 这个函数把对象类型和元数据键作为参数,并返回一个元数据值的数组。

检索指定对象的注册元数据。

结果包括专门为该对象的子类型注册的元数据和为整个对象类型注册的元数据。

function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
	$object_subtype = get_object_subtype( $object_type, $object_id );

	if ( ! empty( $meta_key ) ) {
		if ( ! empty( $object_subtype ) && ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
			$object_subtype = '';
		}

		if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
			return false;
		}

		$meta_keys     = get_registered_meta_keys( $object_type, $object_subtype );
		$meta_key_data = $meta_keys[ $meta_key ];

		$data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] );

		return $data;
	}

	$data = get_metadata( $object_type, $object_id );
	if ( ! $data ) {
		return array();
	}

	$meta_keys = get_registered_meta_keys( $object_type );
	if ( ! empty( $object_subtype ) ) {
		$meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $object_type, $object_subtype ) );
	}

	return array_intersect_key( $data, $meta_keys );
}

常见问题

FAQs
查看更多 >