我一直在尝试通过遵循wordpress codex来搜索用户名,但似乎不起作用。。。
以下查询直接从codex复制,返回0个结果。但是,如果我删除meta\\u查询(或“search”参数,具体取决于我是搜索用户名还是名字),那么它将返回结果。这是Wordpress的bug吗?
从本质上讲,尽管我查阅了法典和多个over示例,明确指出“搜索”和“meta\\u查询”可以同时运行,但事实似乎并非如此。有人能帮我确认一下吗?
$meta_search = \'Ross\';
$qArgs = array (
\'order\' => \'ASC\',
\'orderby\' => \'display_name\',
\'search\' => \'*\'.esc_attr( $meta_search ).\'*\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'first_name\',
\'value\' => $meta_search,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'last_name\',
\'value\' => $meta_search,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'description\',
\'value\' => $meta_search ,
\'compare\' => \'LIKE\'
)
)
);
$find_users = new WP_User_Query($qArgs);
解决方案:由于这里缺少功能,我不得不运行两个单独的查询并将它们合并在一起。虽然离理想状态还很远,但我已经迫不及待地等待bug修复了。因此,就其价值而言(对于更高级别的开发人员来说,这将是最小的,但对于中间产品来说,希望是有用的),以下是我的工作-
// ORIGINAL QUERY
$qArgs = array(\'role\' => \'subscriber\',\'posts_per_page\' => -1);
// SIMPLE SEARCH
$search1Args = array(\'search\' => \'*\'.esc_attr($meta_search).\'*\');
// merge search with original query
$s1Args = array_merge($qArgs, $search1Args);
// go fetch
$find_users_1 = new WP_User_Query($s1Args);
// META QUERY
$search2Args = array(
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'first_name\',
\'value\' => $meta_search,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'last_name\',
\'value\' => $meta_search,
\'compare\' => \'LIKE\'
)
)
);
// merge search with original query
$s2Args = array_merge($qArgs, $search2Args);
// go fetch
$find_users_2 = new WP_User_Query($s2Args);
// MERGE QUERIES
$find_out_1 = $find_users_1->get_results();
$find_out_2 = $find_users_2->get_results();
$found_users = array_merge_recursive($find_out_1, $find_out_2); // rebuild the returned users
$find_count_1 = $find_users_1->total_users;
$find_count_2 = $find_users_2->total_users;
$find_count = ($find_count_1 + $find_count_2); // rebuild the user count
// RECOMPILE
$find_users = $find_users_1;
$find_users->results = $found_users;
$find_users->total_users = $find_count;
作为一个警告,我没有考虑在这里分页,如果有人希望在有大量用户的站点上使用它,那么需要考虑一下。
最合适的回答,由SO网友:birgire 整理而成
他们似乎在一起工作,但请注意AND
在生成的SQL查询中,部分位于搜索部分和元查询部分之间。我想知道你是否期望OR
相反
以下是针对您提到的两种情况生成的SQL查询:
搜索+元查询:
SELECT DISTINCT SQL_CALC_FOUND_ROWS wp_users.*
FROM wp_users
INNER JOIN wp_usermeta
ON ( wp_users.ID = wp_usermeta.user_id )
WHERE 1=1
AND
(
(
wp_usermeta.meta_key = \'first_name\'
AND CAST(wp_usermeta.meta_value AS CHAR) LIKE \'%Ross%\'
)
OR
(
wp_usermeta.meta_key = \'last_name\'
AND CAST(wp_usermeta.meta_value AS CHAR) LIKE \'%Ross%\'
)
OR
(
wp_usermeta.meta_key = \'description\'
AND CAST(wp_usermeta.meta_value AS CHAR) LIKE \'%Ross%\'
)
)
AND
(
user_login LIKE \'%Ross%\'
OR user_nicename LIKE \'%Ross%\'
)
ORDER BY display_name ASC ;
仅搜索:
SELECT SQL_CALC_FOUND_ROWS wp_users.*
FROM wp_users
WHERE 1=1
AND
(
user_login LIKE \'%Ross%\'
OR user_nicename LIKE \'%Ross%\'
)
ORDER BY display_name ASC ;
堆芯检查:在堆芯中,我们有
WP_User_Query::get_search_sql()
方法
returning 一
AND
零件:
return \' AND (\' . implode(\' OR \', $searches) . \')\';
以及
similarly 在
WP_Meta_Query::get_sql_clauses()
方法,我们有一个
AND
零件:
$sql[\'where\'] = \' AND \' . $sql[\'where\'];