最后,我的想法太复杂了。而不是有两个tax_query
一个类别中的数组为什么不首先使用一个类别,然后过滤掉使用条件的帖子。现在看起来是这样的,而且很有效。
<?php
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => array (\'fruits\'),
),
),
);
$catFruits = new WP_Query( $args );
if($catFruits->have_posts()) :
while($catFruits->have_posts()) : $catFruits->the_post();
if (has_term(\'apples\',\'nutrition\')) {
// do stuff
} elseif (has_term(\'oranges\',\'nutrition\')) {
//do stuff
// If has not an array of the terms I don\'t want
// Since I am in category fruits these are all posts without term
] elseif (!has_term(\'apples\', \'oranges\')){
//do stuff
}
endwhile;
endif;
wp_reset_query();
?>
事实上,这是短的,实际上也起到了作用。
<?php
$args = array(
\'post_type\' => \'post\',
\'category_name\' => \'fruits\',
);
$catFruits = new WP_Query( $args );
if($catFruits->have_posts()) :
while($catFruits->have_posts()) : $catFruits->the_post();
if (has_term(\'apples\',\'nutrition\')) {
// do stuff
} elseif (has_term(\'oranges\',\'nutrition\')) {
//do stuff
// If has not an array of the terms I don\'t want
// Since I am in category fruits these are all posts without term
] elseif (!has_term(\'apples\', \'oranges\')){
//do stuff
}
endwhile;
endif;
wp_reset_query();
?>