1

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.

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

1 Answer 1

2

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.

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.