how to get list of all users and their metadata

How do I get a list of all users with role=”Customers” including all the metadata per user, means wp_users + wp_usermeta.

Query blow does not generate the wanted results.

$query = "SELECT * FROM wp_users INNER JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id ORDER BY ID DESC');  ";
$data = $wpdb->get_results($query,ARRAY_A);

But that list is not correct. It shows each user x times depending on the number of rows in wp_usermeta.

How can I filter it? That each user shows per record all his user + metadata?


I think you should use wp-api functions which will do everything for you.

  • get_users() function will get all users data just define fields which u require.
  • get_user_meta() function will get usermeta data.
$users = get_users( array( 'fields' => array( 'ID' ) ) );
foreach($users as $user){
        print_r(get_user_meta ( $user->ID));

Leave a Comment