我想使用WP\\u Query显示两篇文章,一篇来自类别X,另一篇不在类别X中。是否可以创建一个这样的查询,或者我必须查询两次?如果可能,我如何创建一个查询来提供我想要的结果?
这是我目前的参数数组:
$args = array(
\'post_type\' => \'blog\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'bloggcat\',
\'field\' => \'slug\',
\'terms\' => \'appar\',
)
),
\'posts_per_page\' => \'2\',
);
这当然不是我想要的,但我该怎么办呢?
--编辑--
我设法找到了一个解决方案,最终得到了一个适合我的合并查询。我不确定这是最好的解决方案,所以如果有人知道更好的方法,请提供帮助。
在这个解决方案中,我使用不同的参数创建两个WP\\u查询,然后将它们合并,如下所示:
$podArgs = array(
\'post_type\' => \'blog\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'bloggcat\',
\'field\' => \'slug\',
\'terms\' => \'appar\',
)
),
\'posts_per_page\' => \'1\',
);
$notPodArgs = array(
\'post_type\' => \'blog\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'bloggcat\',
\'field\' => \'slug\',
\'terms\' => \'appar\',
\'operator\' => \'NOT IN\'
),
),
\'posts_per_page\' => \'1\',
);
$pods = new WP_Query($podArgs);
$notPods = new WP_Query($notPodArgs);
$blogs = new WP_Query();
$blogs->posts = array_merge( $pods->posts, $notPods->posts );
$blogs->post_count = $pods->post_count + $notPods->post_count;