我正在尝试编写一个使用我创建的几个自定义用户元字段的搜索,但在实现它们时遇到了困难。基本上有相同的问题this guy 我也解决了这个问题。
$exclude = array( \'1\' );
$args1 = array(
\'exclude\' => $exclude,
\'search\' => \'*\' . $search_query . \'*\',
\'search_columns\' => array(
\'user_login\',
\'user_nicename\',
\'user_email\',
\'user_url\',
),
);
$args2 = array(
\'exclude\' => $exclude,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'first_name\',
\'value\' => $search_query,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'last_name\',
\'value\' => $search_query,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'nickname\',
\'value\' => $search_query,
\'compare\' => \'LIKE\'
),
),
);
// queries
$search_query = new WP_User_Query( $args1 );
$meta_query = new WP_User_Query( $args2 );
$user_query = new WP_User_Query();
$user_query->results = array_merge( $search_query->results, $meta_query->results );
//populate total_users count for the loop to work correctly
$user_query->total_users = $search_query->total_users + $meta_query->total_users;
// Remove duplicate users from array
/* final step goes here */
然而,现在,当搜索词与他们的共同用户重叠时,查询有时会返回两次相同的结果。有没有办法筛选重复项?
Edit: 下面是我如何解决这个问题的。
if (!empty($search_query->results)) {
foreach ($search_query->results as $user) {
$user_info = $user->ID;
array_push($exclude, $user_info);
}
}