1

I have an array of user IDs, I want to get data for each of these users. I first thought of writing a classic SQL query but I found WordPress has integreted functions for it. However, get_users(...) is only returning me 1 users though it should return 3. What am I doing wrong?

var_dump($targetUsersIDs);
$targetUsers =  get_users(['include' => $targetUsersIDs]);
var_dump($targetUsers);

Output of var_dump($targetUsersIDs);

array (size=3)
  0 => 
    object(stdClass)[4785]
      public 'ID' => string '1' (length=1)
  1 => 
    object(stdClass)[4784]
      public 'ID' => string '2' (length=1)
  2 => 
    object(stdClass)[4783]
      public 'ID' => string '4' (length=1)

Start of the output of var_dump(targetUsers);

array (size=1)
  0 => 
    object(WP_User) ...
3

The include key on get_users requires an array of IDs (numbers). You are giving it an array of objects that have an ID property. If you look at your first var dump you will see this. WP is casting that to a number and returning the user with that number which is not what you want.

  • I guess it's the right path ... now, looping on the array for($i=0; $i < $targetUsersIDsCount;$i++) { $integerTargetUsersIDs[$i] = (int)$targetUsersIDs[$i]; } Gives me and array of 3 times 1 (integer) – TTT Apr 9 at 10:57
  • 1
    @TTT Instead of (int)$targetUsersID[$i] try $targetUsersIDs[$i]->ID. This means: take the ID property of the object in the index $i of the $targetUsersIDs array. Let me know if that helps. – Julian Apr 9 at 23:34
  • Ah thank you ... now I'm wondering how I didn't see it in the var_dump yesterday. However, somebody gave a more WordPress specific 1-line solution, see the Answer I posted yesterday. – TTT Apr 10 at 12:02
1

Somebody has posted this solution and then deleted their post:

$targetUsers =  get_users(['include' => wp_list_pluck($targetUsersIDs,'ID')]);

It is where I'm using right now.

Please dn't hesitate to tell me if there's any reason it was wrong (I'm not sure the user has deleted their answer).

  • Julian's answer explains you why you only got one user returned. Now there's wp_list_pluck() with which you get an array existing merely of user IDs. Which is exactly what include is expecting. Wondering myself why the answer got deleted. It looks just fine. Maybe they were worried about where the $targetUsersIDs array came from in the first place. – leymannx Apr 9 at 12:57
  • 1
    @TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution. – Julian Apr 9 at 23:36
0

Do it like this

var_dump($targetUsersIDs);

$ids = array();
foreach ( $targetUsersIDs as $id ) $ids[] = $id;

$targetUsers =  get_users(['include' => $ids ] );
var_dump($targetUsers);

I hope this may help.

0

You should be using WP_User_Query for this.

$user_ids = [ 1, 2, 3, 4, 5 ];

$args = [
  'include' = $user_ids,
]

$user_query = new WP_User_Query( $args );

Now you can simply use the result in a user loop/foreach.

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.