最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - How to get user ID's from multiple usernames?

programmeradmin2浏览0评论

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
  • There's nothing wrong with your code. What do you think is wrong with it? – Otto Commented Jun 13, 2019 at 18:17
  • @Otto Since user_login (user1) = id (1) and user_login (user2) = id (2) I need the user ID's to be produced as '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
Add a comment  | 

1 Answer 1

Reset to default 1

Your 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,
);
发布评论

评论列表(0)

  1. 暂无评论