作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想给出 tax_query
和 meta_query
之间的或关系:
$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);
我的代码有什么问题?
最佳答案
将您的 meta_query 放在 tax_query 之后,并使用“get_meta_sql”过滤器包装 WP_Query,然后先将 AND 替换为 OR,如下所示:
add_filter('get_meta_sql', 'filter_query', 10, 1);
$posts = new WP_Query($post_args);
remove_filter('get_meta_sql', 'filter_query');
然后是过滤函数,它将第一个AND替换为OR:
function filter_query($sql){
$pos = strpos($sql['where'], 'AND');
if ($pos !== false) {
$sql['where'] = substr_replace($sql['where'], 'OR', $pos, strlen('AND'));
}
return $sql;
}
这样速度更快,并且可以节省您在合并查询的帖子后过滤重复项的时间。
关于wordpress - 如何在 tax_query 和 meta_query Wordpress 中给出 OR 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43734845/
我是一名优秀的程序员,十分优秀!