函数文档

get_user_meta()

💡 云策文档标注

概述

get_user_meta() 函数用于检索指定用户的元数据字段。它接受用户ID、元键和是否返回单个值等参数,并返回相应的元数据值或数组。

关键要点

  • 参数:$user_id(必需,用户ID),$key(可选,元键,默认为空以返回所有键),$single(可选,是否返回单个值,默认为false)。
  • 返回值:根据$single参数返回数组或单个值;无效$user_id返回false;非存在用户ID根据$single返回空数组或空字符串。
  • 非序列化值作为字符串返回:false返回空字符串,true返回'1',数字返回字符串,数组和对象保持原始类型。
  • 注意事项:元值存在但为空时返回空字符串或数组,可能导致意外行为;使用add_user_meta而非update_user_meta以避免误判。

代码示例

// 获取用户ID为9的所有元数据
$all_meta_for_user = get_user_meta(9);
print_r($all_meta_for_user);

// 获取用户ID为9的last_name元数据(单个值)
$user_last = get_user_meta(9, 'last_name', true);
echo 'The last_name value for user id 9 is: ' . $user_last;

// 使用array_map处理所有元数据以模拟$single效果
$all_meta_for_user = array_map(function($a) { return $a[0]; }, get_user_meta($user_id));
print_r($all_meta_for_user);

注意事项

  • 当$key为空时,返回的元数据值不会自动反序列化,可能需要使用maybe_unserialize()。
  • 检查返回值是否为空时,可使用empty()函数,适用于数组和单个值。
  • 避免使用已弃用的函数如get_currentuserinfo(),推荐使用wp_get_current_user()。

📄 原文内容

Retrieves user meta field for a user.

Parameters

$user_idintrequired
User ID.
$keystringoptional
The meta key to retrieve. By default, returns data for all keys.
$singlebooloptional
Whether to return a single value.
This parameter has no effect if $key is not specified.

Default:false

Return

mixed An array of values if $single is false.
The value of meta data field if $single is true.
False for an invalid $user_id (non-numeric, zero, or negative value).
An empty array if a valid but non-existing user ID is passed and $single is false.
An empty string if a valid but non-existing user ID is passed and $single is true.
Note: Non-serialized values are returned as strings:

  • false values are returned as empty strings ('')
  • true values are returned as '1'
  • numbers (both integer and float) are returned as strings Arrays and objects retain their original type.

More Information

Please note that if the meta value exists but is empty, it will return an empty string (or array) as if the meta value didn’t exist. This might cause unexpected behaviors in your code when you empty the user meta, your code can try to use add_user_meta instead of update_user_meta thinking the user does not have meta created yet.

Source

function get_user_meta( $user_id, $key = '', $single = false ) {
	return get_metadata( 'user', $user_id, $key, $single );
}

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 7 content

    Getting all meta data
    This example demonstrates leaving the $key argument blank, in order to retrieve all meta data for the given user (in this example, user_id = 9):

    Results:

    Array ( [first_name] => Array ( [0] => Tom ) [last_name] => Array ( [0] => Auger)<br />
    [nickname] => Array ( [0] => tomauger ) [description] => etc.... )

    Note: In order to access the data in this example you need to dereference the array that is returned for each key, like so:

    $last_name = $all_meta_for_user['last_name'][0];

    To avoid this, you may want to run a simple array_map() on the results of get_user_meta() in order to take only the first index of each result (thus emulating what the $single argument does when $key is provided:

      $all_meta_for_user = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user_id ) );
      print_r( $all_meta_for_user );

    Results:

    Array ( [first_name] => Tom [last_name] => Auger [nickname] => tomauger [description] => etc.... )

    Additionally, if you want to return ALL meta for a specific user and filter out empty values, you can run array_filter() on the results of the array_map() above:

    // Get all user meta data for $user_id
    $meta = get_user_meta( $user_id );
    
    // Filter out empty meta data
    $meta = array_filter( array_map( function( $a ) {
    	return $a[0];
    }, $meta ) );

  2. Skip to note 10 content

    To check if returned value is empty, ie does not exist, you could use something like:

    global $current_user; 
    
    get_currentuserinfo();
    
    if ( $current_user ) {
    	$permission = get_user_meta( $current_user->ID, 'some_meta' , true );
    	
    	if ( ! empty( $permission ) ) {
    		// do stuff
    	}
    }
    // works for both array and single values