wordpress - 如何查询没有两个标签的帖子?
问题描述
假设我有两个标签,red
并且small
. 我想获得所有不是red
and small
的帖子。我想包含要么是 要么 的帖子,但 它们不能同时是两者。red
small
该tag__not_in
参数不起作用,因为它将排除只是red
或只是small
.
我也尝试了下面的税务查询,但后来意识到它与上面的相同。
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'red',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'small',
'operator' => 'NOT IN',
),
)
我觉得必须有一种方法可以做到这一点,而我只是缺少一些简单的东西。那,我对逻辑反转感到困惑。有任何想法吗?
解决方案
你可以再试一次
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'red',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'small',
),
)
);
$query_red_small = new WP_Query( $args );
$post_ids = wp_list_pluck( $query_red_small->posts, 'ID' );
$args = array(
'post_type' => 'post',
'post__not_in' => $post_ids
);
$query = new WP_Query( $args );
?>
推荐阅读
- git - 从 GIT 提交计算 LOC
- c# - 我们如何使用 C# 单元测试中的 moq 对象来测试数据库中的存储过程?
- python - Python在文本字段中突出显示searchValue
- swift - 如何在 Xcode 单元测试中以 100% 的代码覆盖率编写 do {} catch {}
- java - 在 Linux 服务器上加载 OpenCV 库的问题:文件太短
- powershell - 大规模测试连接 powershell
- ethereum - 使用 Kaleido 的以太坊账户的私钥
- highcharts - 图表边缘上的 3D 圆环图点击事件问题
- javascript - Node中的日期格式
- c# - 如何在文件上使用 LINQ 获取行数