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$keyis not specified.
Default:
false
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. |
Skip to note 6 content
Giulio Daprela
If the key does not exist the function will return an empty string or an empty array depending on the value of the $single parameter
falseif! is_numeric( $user_id )or! absint( $user_id ).Skip to note 7 content
Codex
Getting all meta data
This example demonstrates leaving the
$keyargument 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 ofget_user_meta()in order to take only the first index of each result (thus emulating what the$singleargument does when$keyis 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 thearray_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 ) );Skip to note 8 content
Codex
This example returns and then displays the last name for user id 9.
The '. $key . ' value for user id ' . $user_id . ' is: ' . $user_last . '</p>'; ?>Result:
The last_name value for user id 9 is Franklin
Skip to note 9 content
cartpauj
Note: When doing
get_user_meta( $user_id );without a meta key, the values are not unserialized automatically. You will need tomaybe_unserialize()them.Skip to note 10 content
Goran87
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