如何从多个用户名中获取用户ID?

时间:2019-06-13 作者:Michael

我正在尝试从多个特定用户名生成用户ID。用户名从概要文件字段中提取,并将调用任意数量的用户名。我希望将每个用户的用户ID放入$args的“include”中,如\'include\' => array( 1, 2),. 我做错了什么?

// 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,
);
我试着打印每个变量,看看哪里出错了,下面是它们的输出:

$usernames = Array
$prof_ids = Array
$prof_id = user2
$user = 
$args = Array

1 个回复
最合适的回答,由SO网友:ChristopherJones 整理而成

您的呼叫get_user_by() 有一个小嗝。它必须是login 而不是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,
);