Warning: Trying to access array offset on false in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 216
get_metadata() - 检索指定对象类型和ID的meta数据字段的值 - WordPress函数 - WordPress动力
当前浏览:首页 / WordPress函数 / get_metadata()

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

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


Warning: Undefined array key -1 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 185

Warning: Undefined array key 2 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 186

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "cf56aca0d759bae8c37cbb9d06b846ae" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

metadatamore...


描述

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

如果meta字段不存在,则结果取决于get_metadata_default()。默认情况下,如果$single为true,则返回空字符串;如果为false,则返回空数组。

另见


参数

$meta_type

(string) (必填) 对象meta数据的类型,接受带有关联meta表的“post”、“comment”、“term”、“user”或任何其他对象类型。

$object_id

(int) (必填) 对象meta数据的ID。

$meta_key

(string) (可选) meta数据键名。如果未指定,将检索指定对象的所有meta数据。

默认值: ''

$single

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

默认值: false


返回

(mixed) 如果$single为false,则返回值的数组。如果$single为true,则为元字段的值。对于无效的$object_id(非数字、零值或负值),或如果未指定$meta_type,则为False。如果传递了有效但不存在的对象ID,则为空字符串。



源码

查看源码 官方文档


更新日志

版本描述
2.9.0开始引入

使用示例

  • 示例1

    如果meta值不存在且true == $single ,则函数将返回空字符串('')。如果false == $single ,则返回空数组。

    如果! is_numeric( $user_id )或! absint( $user_id ),则返回布尔值false。

  • 示例2

    post meta数据返回的数组示例:

    array (
      '_pingme' => array (
        0 => '1',
      ),
      '_encloseme' => array (
        0 => '1',
      ),
      'custom key 1' => array (
        0 => 'custom value 1',
        1 => 'custom value 2',
      ),
      'custom key 2' => array (
        0 => 'custom value 3',
      ),
    )