在类别下“;“水果”;我的帖子中有“条款”;“苹果”"E;橙子“;除此之外,没有任何术语。
如何显示类别中的所有帖子;水果“;,包括按时间顺序排列有或没有任何一个术语的帖子?
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
\'relation\' => \'OR\',
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => array (\'fruits\'),
),
array(
\'taxonomy\' => \'nutrition\',
\'field\' => \'slug\',
\'terms\' => array (
\'apples\',
\'oranges\'
),
),
),
);
$catFruits = new WP_Query( $args );
if($catFruits->have_posts()) :
while($catFruits->have_posts()) : $catFruits->the_post();
// https://codex.wordpress.org/Function_Reference/has_term
// https://wordpress.stackexchange.com/a/206166/77054
// <?php has_term( $term, $taxonomy, $post )
if (has_term(\'apples\',\'nutrition\')) {
// do stuff
}
if (has_term(\'oranges\',\'nutrition\')) {
// do stuff
else {
// show all posts in category fruits that have neither term apples or oranges
}
正如你所看到的,根据条款,我喜欢对帖子做不同的事情。但如何显示该类别中的所有帖子;“水果”;以这种方式按时间顺序排列,如果是最新的帖子,顺序是什么?目前在水果类中,上述$args
仅显示带有术语的帖子,但缺少类别中没有任何术语的其余帖子。
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Wordpress tax_query "and" operator not functioning as desired
Show posts without term
https://wordpress.stackexchange.com/a/252102/77054