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