0

I want to get all users with their respective data. I already get all the users:

$users = array();

$users_query = new WP_User_Query( array( 
    'role' => 'subscriber', 
    'orderby' => 'user_registered',
    'number' => 8,
    'order' => 'DESC',
    'fields' => array('ID', 'display_name')
) );
$results = $users_query->get_results();

But I only get the data from user table:

object(stdClass)#3162 (10) {
      ["ID"]=>
      string(2) "44"
      ["user_login"]=>
      string(13) "usuarioprueba"
      ["user_nicename"]=>
      string(13) "usuarioprueba"
      ["user_url"]=>
      string(0) ""
      ["user_registered"]=>
      string(19) "2016-10-13 16:27:56"
      ["user_status"]=>
      string(1) "0"
      ["display_name"]=>
      string(14) "Usuario Prueba"
    } ...

How do I get also the data from usermeta without having to use a loop like a foreach

2

All the metadata should be there, you can access them with magic methods.

If you have a metadatum called with the key shoes, you can access it like this

$userObject->shoes
0

This was under the return fields parameter on WP_User_Query

'all (default) or all_with_meta' - Returns an array of WP_User objects. Must pass an array to subset fields returned.

*'all_with_meta' currently returns the same fields as 'all' which does not include user fields stored in wp_usermeta. You must create a second query to get the user meta fields by ID or use the __get PHP magic method to get the values of these fields.

So, unless this is outdated, it sounds like it's not directly possible.

  • Looking at the code of wp-includes/class-wp-user-query.php, it appears that (at least according to the comments) all_with_meta should return all the users with their metadata. – Pat J Oct 13 '16 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.