admin管理员组

文章数量:1024673

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) ...

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) ...
Share Improve this question edited Apr 9, 2019 at 12:53 norman.lol 3,2413 gold badges30 silver badges35 bronze badges asked Apr 9, 2019 at 10:18 TTTTTT 3291 gold badge4 silver badges17 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 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.

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).

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.

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.

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) ...

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) ...
Share Improve this question edited Apr 9, 2019 at 12:53 norman.lol 3,2413 gold badges30 silver badges35 bronze badges asked Apr 9, 2019 at 10:18 TTTTTT 3291 gold badge4 silver badges17 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 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.

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).

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.

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.

本文标签: phpgetusers() only returns one user