Retrieving user metadata in WordPress using get_user_meta

The get_user_meta function in WordPress retrieves the metadata for a user. This metadata can include a wide range of information about the user, such as their display name, email address, and profile picture. This function can be useful for displaying user-specific information on a website, such as a user’s bio or contact information. It allows developers to easily access and display user metadata without having to manually query the database.

Parameters Accepted by get_user_meta Function

  • $user_id (int, required): User ID.
  • $key (string, optional, default value: ”): The meta key to retrieve. By default, returns data for all keys.
  • $single (bool, optional, default value: false): Whether to return a single value. This parameter has no effect if $key is not specified.

Value Returned by get_user_meta Function

The function returns:

  • 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 string if a valid but non-existing user ID is passed.


How to get a specific user meta value

$user_id = 123;
$meta_key = 'first_name';
$first_name = get_user_meta( $user_id, $meta_key, true );

echo $first_name;

This code snippet retrieves the value of the user meta field ‘first_name’ for the user with ID 123 using the get_user_meta function. The retrieved value is then echoed out.

How to check if a user meta field exists

$user_id = 456;
$meta_key = 'last_login';
$last_login = get_user_meta( $user_id, $meta_key, true );

if ( $last_login ) {
 echo 'Last login: ' . $last_login;
} else {
 echo 'Last login not found';

This code snippet checks if the user meta field ‘last_login’ exists for the user with ID 456 using the get_user_meta function. If the field exists, it echos out the last login value, otherwise it echos out a message indicating that the last login was not found.

How to get all user meta fields for a user

$user_id = 789;
$user_meta = get_user_meta( $user_id );

foreach ( $user_meta as $key => $value ) {
 echo $key . ': ' . $value[0] . '<br>';

This code snippet retrieves all the meta fields for the user with ID 789 using the get_user_meta function. It then loops through the meta fields and echos out each field’s key and value.


In conclusion, the get_user_meta function is a valuable tool for retrieving user meta data in WordPress. It provides a simple and efficient way to access user-specific information, such as custom fields and user preferences. By using this function, developers can streamline their code and improve the overall performance of their WordPress applications. With its flexibility and ease of use, get_user_meta is an essential function for any developer working with user data in WordPress.

Related WordPress Functions