Show all posts without a custom taxonomy

I’ve searched high and low, but haven’t found the exact code to do this.

I want to show all posts of a that don’t have a custom taxonomy assigned.

Read More

So something like, pull all posts that have one of the custom taxonomy, then show everything but those posts.

Related posts

Leave a Reply

1 comment

  1. If you use WP_Query‘s tax_query, you can set the operator to NOT IN and then just list your terms. The best way to do this (IMO) is to generate the tax query separately and dynamically, since all that will be changing in each of the different arguments is the slug/id of the category name. Your code should look something like this:

    $tax_query = array();
    $tax_query['relation'] = 'OR'; // look for any of the following
    $tax_query[] = array(
        'taxonomy' => 'your-taxonomy', // exactly what it sounds like
        'field'    => 'slug or ID', // put either slug or id, depends on what $term is
        'terms'    => $array_of_terms,
        'operator' => 'NOT IN'
    );
    

    Note: I wrote this straight into the editor, it’s totally untested and is more pseudocode than actual code. You will need to provide your own array for $array_of_terms, that’s simple stuff though.

    Once you’ve got that, you can just set 'tax_query' => $tax_query as one of your WP_Query args.