请注意s
查询参数/字符串是为帖子保留的,因此您应该为自定义用户搜索使用不同的名称。
所以基本上,你只需要;“播放”;使用元查询嵌套
是的,就像普通(PHP)数组可以是多维/多级格式一样
meta_query
parameter, 是为了
WP_Query
或者
WP_User_Query
在你的情况下。所以你可以在一个元查询子句中有一个元查询子句在一个元查询子句中。。。好吧,你明白了。:-)
在这个修订版本中,我们需要name参数($s_query
) 始终处于<first name> <last name>
格式类似John Doe
或者也许Jane Doe Jr.
, 因此,例如,如果只指定了名字,那么我们只搜索该meta,如果指定了国家值,则搜索位置meta:
// Parse the first and last names into an array.
$names = preg_split( \'/ +/\', trim( $s_query ), 2 );
// Then, define the meta query variable.
$meta_query = [];
// If a name if specified, add the first and last name queries to the $meta_query.
// So we\'re using a nested meta query clause to search in either the first and last
// name meta.
if ( $s_query ) {
$name_query = [];
if ( ! empty( $names[0] ) ) {
$name_query[] = [ // child clause
\'key\' => \'first_name\',
\'value\' => $names[0],
\'compare\' => \'LIKE\',
];
}
if ( ! empty( $names[1] ) ) {
$name_query[] = [ // child clause
\'key\' => \'last_name\',
\'value\' => $names[1],
\'compare\' => \'LIKE\',
];
}
// Add a top-level clause for $s_query
if ( ! empty( $name_query ) ) {
$name_query[\'relation\'] = \'AND\';
$meta_query[] = $name_query;
}
}
// If a country is specified, add the country query to the $meta_query.
if ( $country ) {
$meta_query[] = [ // top-level clause
\'key\' => \'location\',
\'value\' => $country,
\'compare\' => \'LIKE\',
];
}
// Now set the relationship for the top-level meta query clauses.
$meta_query[\'relation\'] = ( $s_query && $country ) ? \'AND\' : \'OR\';
// Then add the meta query to the main query args (for WP_User_Query).
$args = [
\'meta_query\' => $meta_query,
];