我希望本机WordPress搜索输入除了标准标题和内容搜索之外,还可以搜索元数据,但是设置meta_query
添加AND
当我需要的是OR
.
我的职能:
function search_faqs_metadata($query) {
if(!is_admin() && $query->is_main_query()) {
if($query->is_search) {
$query->set(\'meta_query\', array(array(
\'key\' => \'_faqs\',
\'value\' => $query->query_vars[\'s\'],
\'compare\' => \'LIKE\'
)));
}
}
}
add_action(\'pre_get_posts\', \'search_faqs_metadata\');
问题是它生成的查询:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
WHERE 1=1
AND (
(
(wp_posts.post_title LIKE \'%foobar%\') OR (wp_posts.post_content LIKE \'%foobar%\')
)
)
AND (wp_posts.post_password = \'\')
AND wp_posts.post_type IN (\'post\', \'page\', \'attachment\')
AND (wp_posts.post_status = \'publish\')
AND ( //I need this to be an OR!
(wp_postmeta.meta_key = \'_faqs\' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE \'%foobar%\')
)
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title LIKE \'%foobar%\' DESC, wp_posts.post_date DESC LIMIT 0, 10
因此,除非帖子标题/内容
AND 元数据与搜索词匹配。
我知道有一个relation
的参数meta_query
但它仅在比较多个元数据值时有效。
目前我正在跑步str_replace()
在最后一个查询上更改最后一个AND
变成一个OR
. 我选择这种方法是因为,虽然有黑客行为,但它依赖于我需要添加多少代码才能成功进行调整:
function search_where_or_metadata($where) {
if(is_search()) {
global $wp_query;
$new_where = $where;
$new_where = str_replace(\'AND ( (wp_postmeta.meta_key\', \'OR ( (wp_postmeta.meta_key\', $where);
return $new_where;
}
return $where;
}
add_filter(\'posts_where\', \'search_where_or_metadata\');
EDIT下面是一个更好的函数,它使用更合适的过滤器来更改
AND
到
OR
:
function change_meta_key_where_from_and_to_or($sql) {
if(is_search()) {
$sql[\'where\'] = substr($sql[\'where\'], 1, 3) == \'AND\' ? substr_replace($sql[\'where\'], \'OR\', 1, 3) : $sql[\'where\'];
}
return $sql;
}
add_filter(\'get_meta_sql\', \'change_meta_key_where_from_and_to_or\');
有没有更好的办法改变决赛
AND
变成一个
OR
?