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
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

1 Answer 1

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,
);
1
  • This works perfectly. Thank you!!
    – Michael
    Commented Jun 13, 2019 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.