admin管理员组文章数量:1425245
I am trying to produce user ID's from multiple specific usernames. The usernames are pulled from a profile field, and any number of usernames will be called. I want the user ID's from each of the users to be put into the 'include' of $args like 'include' => array( 1, 2),
. What am I doing wrong?
// Search these Usernames
$usernames = array( user1, user2 );
// Fetch the User IDs
$prof_ids = array();
foreach ($usernames as $prof_id) {
$user = get_user_by('user_login', $prof_id);
$prof_ids[] = $user->ID;
}
// WP_User_Query arguments
$args = array(
'include' => $prof_ids,
);
I tried printing each variable to see where I'm going wrong, and these are their outputs:
$usernames = Array
$prof_ids = Array
$prof_id = user2
$user =
$args = Array
I am trying to produce user ID's from multiple specific usernames. The usernames are pulled from a profile field, and any number of usernames will be called. I want the user ID's from each of the users to be put into the 'include' of $args like 'include' => array( 1, 2),
. What am I doing wrong?
// Search these Usernames
$usernames = array( user1, user2 );
// Fetch the User IDs
$prof_ids = array();
foreach ($usernames as $prof_id) {
$user = get_user_by('user_login', $prof_id);
$prof_ids[] = $user->ID;
}
// WP_User_Query arguments
$args = array(
'include' => $prof_ids,
);
I tried printing each variable to see where I'm going wrong, and these are their outputs:
$usernames = Array
$prof_ids = Array
$prof_id = user2
$user =
$args = Array
Share
Improve this question
asked Jun 13, 2019 at 18:14
MichaelMichael
2811 silver badge14 bronze badges
2
|
1 Answer
Reset to default 1Your call to get_user_by()
has a small hiccup in it. It needs to be login rather than user_login
// Search these Usernames
$usernames = array('user1', 'user2');
// Fetch the User IDs
$prof_ids = array();
foreach ($usernames as $prof_id) {
$user = get_user_by('login', $prof_id);
$prof_ids[] = $user->ID;
}
// WP_User_Query arguments
$args = array(
'include' => $prof_ids,
);
本文标签: loopHow to get user ID39s from multiple usernames
版权声明:本文标题:loop - How to get user ID's from multiple usernames? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745404511a2657191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'include' => array( 1, 2),
, but it's not doing it as expected. In other words,$usernames = array( user1, user2 );
should produce'include' => array( 1, 2),
. My full code works fine if I type in'include' => array( 1, 2),
, but it does not work with this section of code. – Michael Commented Jun 13, 2019 at 18:25