WordPress函数:get_metadata 获取指定的属性值

江河/ 2023年10月03日/ WordPress/ 浏览 494

函数原型:


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


检索指定对象类型和ID的属性字段的值。


如果元字段存在,如果$single为true,则返回单个值,如果为false,则返回一个值数组。


如果元字段不存在,则结果取决于get_metadata_default()。


默认情况下,如果$single为true,则返回一个空字符串;如果为false,则返回空数组。


参数说明:


$meta_type,对象元数据的类型为。接受'post'、'comment'、'term'、'user'或任何其他具有关联元表的对象类型。


$object_id,对象ID。


$meta_key,属性名称。


$single,如果为true,则只返回指定$meta_key的第一个值。如果未指定$meta_key,则此参数无效。默认值:false。


如果$single为false,则为一个值数组。


如果$single为true,则元字段的值。


对于无效的$object_id(非数字、零或负值),或者如果未指定$meta_type,则为False。


如果传递了有效但不存在的对象ID,则为空字符串。


函数源码:


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


使用举例:


get_metadata('post', 100, 'view_count', true);


发表评论

暂无评论,抢个沙发...

客服 工单