get_users(…) only returns one user The 2019 Stack Overflow Developer Survey Results Are In2019 Community Moderator ElectionBest way to get user id for get_users function?Get multiple roles with get_usersget_users is expecting unserialized meta_valueHow to do get_users() with multiple meta_keysHow to get the user description with get_users?get_users meta_querySort get_users by custom fieldget_users() ORDER BY Not WorkingGet_Users Orderby Pageget_users by role returns all users
Correct punctuation for showing a character's confusion
Is it okay to consider publishing in my first year of PhD?
Accepted by European university, rejected by all American ones I applied to? Possible reasons?
If I score a critical hit on an 18 or higher, what are my chances of getting a critical hit if I roll 3d20?
Why isn't the circumferential light around the M87 black hole's event horizon symmetric?
Can an undergraduate be advised by a professor who is very far away?
Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?
Are spiders unable to hurt humans, especially very small spiders?
Is it a good practice to use a static variable in a Test Class and use that in the actual class instead of Test.isRunningTest()?
Cooking pasta in a water boiler
How to support a colleague who finds meetings extremely tiring?
What to do when moving next to a bird sanctuary with a loosely-domesticated cat?
Falsification in Math vs Science
Why are there uneven bright areas in this photo of black hole?
Inverse Relationship Between Precision and Recall
Can there be female White Walkers?
Pokemon Turn Based battle (Python)
What does もの mean in this sentence?
Dropping list elements from nested list after evaluation
Mathematics of imaging the black hole
Why does the nucleus not repel itself?
If my opponent casts Ultimate Price on my Phantasmal Bear, can I save it by casting Snap or Curfew?
Why couldn't they take pictures of a closer black hole?
What is this business jet?
get_users(…) only returns one user
The 2019 Stack Overflow Developer Survey Results Are In2019 Community Moderator ElectionBest way to get user id for get_users function?Get multiple roles with get_usersget_users is expecting unserialized meta_valueHow to do get_users() with multiple meta_keysHow to get the user description with get_users?get_users meta_querySort get_users by custom fieldget_users() ORDER BY Not WorkingGet_Users Orderby Pageget_users by role returns all users
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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) ...
php id get-users
New contributor
add a comment |
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) ...
php id get-users
New contributor
add a comment |
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) ...
php id get-users
New contributor
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) ...
php id get-users
php id get-users
New contributor
New contributor
edited 2 days ago
leymannx
86611122
86611122
New contributor
asked 2 days ago
TTTTTT
1628
1628
New contributor
New contributor
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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.
New contributor
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
2 days ago
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
2 days ago
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
yesterday
add a comment |
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).
New contributor
Julian's answer explains you why you only got one user returned. Now there'swp_list_pluck()
with which you get an array existing merely of user IDs. Which is exactly whatinclude
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
2 days ago
1
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
2 days ago
add a comment |
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.
add a comment |
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.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "110"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
TTT is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f333863%2fget-users-only-returns-one-user%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
New contributor
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
2 days ago
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
2 days ago
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
yesterday
add a comment |
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.
New contributor
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
2 days ago
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
2 days ago
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
yesterday
add a comment |
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.
New contributor
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.
New contributor
New contributor
answered 2 days ago
JulianJulian
1313
1313
New contributor
New contributor
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
2 days ago
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
2 days ago
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
yesterday
add a comment |
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
2 days ago
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
2 days ago
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
yesterday
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
2 days ago
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
2 days ago
1
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
2 days ago
@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
2 days ago
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
yesterday
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
yesterday
add a comment |
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).
New contributor
Julian's answer explains you why you only got one user returned. Now there'swp_list_pluck()
with which you get an array existing merely of user IDs. Which is exactly whatinclude
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
2 days ago
1
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
2 days ago
add a comment |
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).
New contributor
Julian's answer explains you why you only got one user returned. Now there'swp_list_pluck()
with which you get an array existing merely of user IDs. Which is exactly whatinclude
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
2 days ago
1
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
2 days ago
add a comment |
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).
New contributor
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).
New contributor
New contributor
answered 2 days ago
TTTTTT
1628
1628
New contributor
New contributor
Julian's answer explains you why you only got one user returned. Now there'swp_list_pluck()
with which you get an array existing merely of user IDs. Which is exactly whatinclude
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
2 days ago
1
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
2 days ago
add a comment |
Julian's answer explains you why you only got one user returned. Now there'swp_list_pluck()
with which you get an array existing merely of user IDs. Which is exactly whatinclude
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
2 days ago
1
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
2 days ago
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
2 days ago
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
2 days ago
1
1
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
2 days ago
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
2 days ago
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited 2 days ago
answered 2 days ago
Qaisar FerozQaisar Feroz
1,4711217
1,4711217
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 2 days ago
leymannxleymannx
86611122
86611122
add a comment |
add a comment |
TTT is a new contributor. Be nice, and check out our Code of Conduct.
TTT is a new contributor. Be nice, and check out our Code of Conduct.
TTT is a new contributor. Be nice, and check out our Code of Conduct.
TTT is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to WordPress Development Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f333863%2fget-users-only-returns-one-user%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown