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);
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 badgesarray (size=1) 0 => object(WP_User) ...
4 Answers
Reset to default 3The 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);
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 badgesarray (size=1) 0 => object(WP_User) ...
4 Answers
Reset to default 3The 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
版权声明:本文标题:php - get_users(...) only returns one user 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745610775a2158990.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论