gpt4 book ai didi

wordpress - 如何在 tax_query 和 meta_query Wordpress 中给出 OR 关系

转载 作者:行者123 更新时间:2023-12-05 02:19:00 26 4
gpt4 key购买 nike

我想给出 tax_querymeta_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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com