如何在税务查询和元查询Wordpress中给出或关联
我想给出如何在税务查询和元查询Wordpress中给出或关联,wordpress,Wordpress,我想给出税务查询和元查询之间的关系: $post_args = array('post_type' => 'post', 'order' => 'DESC', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'client_segment', 'value' =>$client ,
税务查询
和元查询
之间的关系:
$post_args = array('post_type' => 'post',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'client_segment',
'value' =>$client ,
'compare' => 'IN',
),
array(
'key' => 'filtered_date',
'value'=> array($start_date, $end_date),
'compare' => 'BETWEEN'
)
),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $term_id,
'operator' => 'IN')
),
);
$posts = new WP_Query($post_args);
我的代码有什么问题?没有办法给出两个查询之间的关系,但是您可以做些什么来创建两个单独的查询,然后合并它们的结果 因为基本上你需要这两个查询的结果 你可以找到解决办法
将元查询放在税务查询之后,用“get\u meta\u sql”过滤器包装WP\u查询,然后用OR替换第一个和,如下所示:
add_filter('get_meta_sql', 'filter_query', 10, 1);
$posts = new WP_Query($post_args);
remove_filter('get_meta_sql', 'filter_query');
然后是filter函数,它将第一个和替换为或:
function filter_query($sql){
$pos = strpos($sql['where'], 'AND');
if ($pos !== false) {
$sql['where'] = substr_replace($sql['where'], 'OR', $pos, strlen('AND'));
}
return $sql;
}
这会更快,并节省您在合并查询的帖子后过滤重复内容的时间