Using WordPress ‘get_user_metavalues()’ PHP function

The get_user_metavalues() WordPress PHP function retrieves the metavalues array(s) required by the _fill_user and _fill_many_users functions.

Usage

get_user_metavalues( array $ids );

Parameters

  • $ids (array) – An array of User ID numbers that you want to retrieve metavalues for.

More information

See WordPress Developer Resources: get_user_metavalues()

Examples

Retrieve metavalues for a single user

This code retrieves metavalues for a user with an ID of 1.

$user_id = 1;
$metavalues = get_user_metavalues( array( $user_id ) );

Retrieve metavalues for multiple users

This code retrieves metavalues for users with IDs 1, 2, and 3.

$user_ids = array( 1, 2, 3 );
$metavalues = get_user_metavalues( $user_ids );

Retrieve metavalues for all users

This code retrieves metavalues for all users in the WordPress database.

$all_users = get_users();
$user_ids = array_map( function( $user ) {
    return $user->ID;
}, $all_users );
$metavalues = get_user_metavalues( $user_ids );

Retrieve specific metavalues for a user

This code retrieves specific metavalues for a user with an ID of 1.

$user_id = 1;
$metavalues = get_user_metavalues( array( $user_id ) );
$specific_metavalues = array_filter( $metavalues, function( $meta ) {
    return $meta->meta_key == 'my_custom_meta_key';
} );

Count metavalues for a user

This code counts the metavalues for a user with an ID of 1.

$user_id = 1;
$metavalues = get_user_metavalues( array( $user_id ) );
$count = count( $metavalues );