Wordpress - Show posts without term
You cannot use the same object of WP_Query
twice. Therefore you need to create another one with a tax_query
parameter to fetch posts which are not assigned to any term.
//fetch all reviews which have no assigned term in 'review-product'
$taxonomy = 'review-product';
$post_type = 'reviews';
$args = [
'post_type' => $post_type,
'tax_query' => [
[
'taxonomy' => $taxonomy,
'terms' => get_terms( $taxonomy, [ 'fields' => 'ids' ] ),
'operator' => 'NOT IN'
]
]
];
$query = new \WP_Query( $args );
The idea is to fetch a list of all terms of your taxonomy and pass them as argument to your tax-query with the NOT IN
operator.
The second loop in your example should walk over the new WP_Query
object.
$posts_query = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'foo',
'operator' => 'NOT EXISTS', // or 'EXISTS'
),
),
) );
see: https://core.trac.wordpress.org/ticket/29181