get_users(…) only returns one user
.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
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 11 hours ago
leymannx
78611022
78611022
New contributor
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 13 hours ago
TTTTTT
1357
1357
New contributor
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
Julian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
13 hours 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
40 mins 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
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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 whatincludeis expecting. Wondering myself why the answer got deleted. It looks just fine. Maybe they were worried about where the$targetUsersIDsarray came from in the first place.
– leymannx
11 hours ago
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
38 mins 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
Julian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
13 hours 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
40 mins ago
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
Julian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
13 hours 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
40 mins ago
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
Julian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
Julian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Julian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 13 hours ago
JulianJulian
1313
1313
New contributor
Julian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Julian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Julian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
13 hours 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
40 mins ago
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
13 hours 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
40 mins 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
13 hours 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
13 hours 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
40 mins 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
40 mins 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
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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 whatincludeis expecting. Wondering myself why the answer got deleted. It looks just fine. Maybe they were worried about where the$targetUsersIDsarray came from in the first place.
– leymannx
11 hours ago
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
38 mins 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
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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 whatincludeis expecting. Wondering myself why the answer got deleted. It looks just fine. Maybe they were worried about where the$targetUsersIDsarray came from in the first place.
– leymannx
11 hours ago
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
38 mins 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
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 11 hours ago
TTTTTT
1357
1357
New contributor
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
TTT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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 whatincludeis expecting. Wondering myself why the answer got deleted. It looks just fine. Maybe they were worried about where the$targetUsersIDsarray came from in the first place.
– leymannx
11 hours ago
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
38 mins 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 whatincludeis expecting. Wondering myself why the answer got deleted. It looks just fine. Maybe they were worried about where the$targetUsersIDsarray came from in the first place.
– leymannx
11 hours ago
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
38 mins 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
11 hours 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
11 hours ago
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
38 mins ago
@TTY this is just a shortcut for what I stated in my comment. Nothing wrong with either solution.
– Julian
38 mins 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 13 hours ago
answered 13 hours ago
Qaisar FerozQaisar Feroz
1,4191217
1,4191217
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 11 hours ago
leymannxleymannx
78611022
78611022
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