在 wp_query 中结合关键字搜索和税务查询 [英] combine keyword search and tax query in wp_query
问题描述
我一直在尝试创建自定义搜索查询,并在这方面取得了一些进展,但又遇到了另一个障碍.
I've been trying to create a custom search query, and I've made some progress on it, but have hit another roadbump.
我正在尝试将 wp_query 中的 meta_query、关键字搜索(s")和 tax_query 与OR"关系结合起来.
I'm trying to combine the meta_query, keyword search ('s') and tax_query in a wp_query with an 'OR' relationship.
多亏了这篇精彩的帖子,我已经让 meta_query 和 's' 协同工作:https://wordpress.stackexchange.com/questions/99849/search-that-will-look-in-custom-field-post-title-and-post-content
I've gotten the meta_query and 's' to work together thanks to this fantastic post: https://wordpress.stackexchange.com/questions/99849/search-that-will-look-in-custom-field-post-title-and-post-content
但是,tax_query 仍然给我带来麻烦.我尝试通过相同的方法添加它,但似乎 wordpress 在将它输出到 SQL 查询之前对 tax_query 做了一些其他魔术.
however, the tax_query is still giving me trouble. I tried adding it in via the same method, but it seems as though wordpress does some other magic with tax_query before it outputs it to the SQL query.
这是我目前得到的:
function add_join_wpse_news($joins)
{
global $wpdb;
return $joins . " INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)" ;
}
function alter_search_wpse_news($search,$qry)
{
global $wpdb;
$add = $wpdb->prepare("({$wpdb->postmeta}.meta_key = '_et_builder_settings' AND CAST({$wpdb->postmeta}.meta_value AS CHAR) LIKE '%%%s%%')",$qry->get('s'));
$pat = '|\(\((.+)\)\)|';
$search = preg_replace($pat,'(($1 OR '.$add.'))',$search);
return $search;
}
function alter_groupby_wpse_news($groupby)
{
global $wpdb;
$mygroupby = "{$wpdb->posts}.ID";
if( preg_match( "/$mygroupby/", $groupby )) {
// grouping we need is already there
return $groupby;
}
if( !strlen(trim($groupby))) {
// groupby was empty, use ours
return $mygroupby;
}
// wasn't empty, append ours
return $groupby . ", " . $mygroupby;
}
add_filter('posts_join','add_join_wpse_news');
add_filter('posts_search','alter_search_wpse_news',1,2);
add_filter('posts_groupby', 'alter_groupby_wpse_news' );
$args_condensed = array
(
'post_type' => 'news',
'paged' => $paged,
's' => $getname,
);
$the_query = new WP_Query($args_condensed);
$max_pages = $the_query->max_num_pages;
echo $GLOBALS['the_query']->request;
这是有效的.但是,它不包括对标签或类别的搜索.我尝试通过posts_join和posts_search过滤器手动添加它,但后来我意识到wordpress在输出SQL查询之前比较tax_query中的值,这在尝试添加时会导致问题.
And this works. However, it doesn't include the search for the the Tags or Categories. I attempted to add it in manually via the posts_join and posts_search filter, but then I realized that wordpress is comparining values in the tax_query BEFORE the outputted SQL query, which causes problems when trying to add it in.
任何帮助将不胜感激.
为了澄清起见,我正在尝试添加:
for clarification, I'm trying to add in:
'tax_query' => array
(
'relation' => 'OR',
array //Search Tag
(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => array($getname)
),
array //Search Category
(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($getname),
),
array //Search Category (Single Words)
(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => explode(" ",$getname),
),
array //Search Tag (Single Words)
(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => explode(" ",$getname),
)
),
但使用 OR 类型的关系,而不是 wordpress 默认添加的 AND 关系.
but with an OR type relationship as opposed to the AND relationship wordpress adds by default.
推荐答案
您无法将 tax_query 用于此目的.您必须覆盖 wordpress 提供的过滤器才能实现此任务.这是我的代码.希望能帮到你:
There are no way you can use tax_query for this purpose. You must override filter that wordpress provided to achieve this mission. Here is my code. Hope it help:
function add_join_wpse_news($joins)
{
global $wpdb;
$joins = $joins . " INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)" ;
$joins .= " inner join {$wpdb->term_relationships} as wrel on {$wpdb->posts}.ID = wrel.object_id";
$joins .= " inner join {$wpdb->term_taxonomy} as wtax on wtax.term_taxonomy_id = wrel.term_taxonomy_id";
$joins .= " inner join {$wpdb->terms} as wter on wter.term_id = wtax.term_id";
return $joins;
}
function add_where_wpse_news($where) {
$getname = 'what you want';
return $where. ' AND '. "wter.slug like '%$getname%' ";
}
add_filter('posts_join','add_join_wpse_news');
add_filter('posts_where','add_where_wpse_news');
我只是添加了 posts_where 并修改了 posts_join 过滤器.
I just add posts_where and modify posts_join filter.
这篇关于在 wp_query 中结合关键字搜索和税务查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!