• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

関数リファレンス/get user meta

提供: WordPress Codex 日本語版
< 関数リファレンス
2017年5月5日 (金) 16:41時点におけるGblsm (トーク | 投稿記録)による版 (en:Function Reference/get_user_meta 07:43, 14 December 2016 Acafourek 版を流し込み。)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

このページ「関数リファレンス/get user meta」は未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

説明

Retrieve a single meta field or all fields of user_meta data for the given user. Uses get_metadata(). This function replaces the deprecated get_usermeta() function.

使い方

<?php get_user_meta($user_id, $key, $single); ?>

パラメータ

$user_id
integer) (必須) The ID of the user whose data should be retrieved.
初期値: なし
$key
string) (optional) The meta_key in the wp_usermeta table for the meta_value to be returned. If left empty, will return all user_meta fields for the given user.
初期値: (empty string)
$single
boolean) (optional) If true return value of meta data field, if false return an array. This parameter has no effect if $key is left blank.
初期値: false

戻り値

(mixed) 
Will be an Array if $key is not specified or if $single is false. Will be value of meta_value field if $single is true.
NOTE
If the meta value does not exist and $single is true the function will return an empty string. If $single is false an empty array is returned.

用例

This example returns and then displays the last name for user id 9.

<?php 
  $user_id = 9;
  $key = 'last_name';
  $single = true;
  $user_last = get_user_meta( $user_id, $key, $single ); 
  echo '<p>The '. $key . ' value for user id ' . $user_id . ' is: ' . $user_last . '</p>'; 
?>
The last_name value for user id 9 is Franklin


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):

<?php
  $all_meta_for_user = get_user_meta( 9 );
  print_r( $all_meta_for_user );
?>

Results:

Array ( [first_name] => Array ( [0] => Tom ) [last_name] => Array ( [0] => Auger) [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 (this 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 ) );

参考

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 behaviours 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.


変更履歴

Since: 3.0

ソースファイル

get_user_meta() is located in wp-includes/user.php.

関連項目

add_user_meta(), delete_user_meta(), get_user_meta(), update_user_meta(), get_user_option() /en, delete_user_option() /en, update_user_option() /en,


関数リファレンステンプレートタグ目次もご覧ください。


最新英語版: WordPress Codex » Function_Reference/get_user_meta最新版との差分