0

Under a category "fruits" I have posts with terms "apples", "oranges" and without any terms but in that category.

How do I display all posts from the category "fruits", including posts that have either one of or none of the terms all in chronological order?

    $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
            }

As you can see according to the terms I like to do different things with the posts. But how can I display all posts from the category "fruits" in this manner in a chronological, latest post if first, order?

Right now in category fruits with the above $args only the posts with a term are displayed but the rest of the posts in the category without any term are missing.

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 Answer 1

0

In the end I was thinking too complicated. Instead of having two tax_query arrays inside a single category why not just take the single category for starters and then filter out the posts that have terms with the use of conditionals. It now looks like this and works.

<?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();
?>

And in fact this is shorter and actually does the trick as well.

<?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();
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.