按时间顺序显示包含和不包含术语的类别中的所有帖子

时间:2017-01-13 作者:lowtechsun

在类别下“;“水果”;我的帖子中有“条款”;“苹果”"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

1 个回复
最合适的回答,由SO网友:lowtechsun 整理而成

最后,我的想法太复杂了。而不是有两个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();
?>