admin管理员组文章数量:1321605
I'm using default search with get_search_form() function. I want to search also by custom fields, so I wrote a hook:
function search_products($query) {
if (is_search() && $query->is_main_query() && !is_admin()) {
$query->set('post_type', 'product');
$my_custom_field = $query->query['s'];
$meta_query = array(
array(
'key' => 'custom_field',
'value' => $my_custom_field,
'compare' => 'LIKE',
)
);
$query->set('meta_query', $meta_query);
}
}
add_action('pre_get_posts', 'search_products');
Problem is the search is not showing products filtered by title and also not showing products filtered by custom field. How to solve this?
本文标签: wp querymetaquery is overriding default search
版权声明:本文标题:wp query - meta_query is overriding default search 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742103034a2420889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论