Nested Term Query shows all Terms when none are selected


I use a nested Term Query. When a post is not assigned to any term the Query will display all terms registered with the Taxonomy.
This only happens when no Term of any Taxonomy is selected.

I have two taxonomies: Location and Genre (only these two are assigned to my CPT, nothing else)
The Term Query displays terms of the Location taxonomy. When I don’t select any Term of both available taxonomies the Query will display all terms of the Location taxonomy. As soon as I select a Term of the Genre taxonomy the Query works and will be empty.

Step-by-step reproduction instructions:

  1. Use a CPT and assign at least two custom taxonomies (TaxonomyA and TaxonomyB)
  2. Create a Term Query inside a Post Query that calls your CPT
  3. Set the nested Term Query to only include Post Terms of TaxonomyA
  4. Create a CPT without selecting any Term
  5. Your Term Query should now display all Terms registered with your TaxonomyA
  6. Now edit your post and select a Term of TaxonomyB
  7. The Term Query now is empty, as it should be

Environment info

  • WordPress version: latest
  • Cwicly Plugin version: latest

Hi @Jonas,

Thank you for bringing this to our attention!

I have been able to reproduce this error on my end, and can confirm that all terms showing when no term has been selected is a bug.

Just to make sure we’re on the same page, is this how you have set up your Terms Query?

Thank you in advance, and apologies for the inconvenience.

Hi @Araminta,

thanks for getting back.
Yes, that’s how I set up the query :+1:

1 Like

I just noticed that this issue also appears if I use a Term Query in a Single Post Template. So it seems it is the issue is not the nested query.


Reviewing the WordPress class, this looks like the intended behaviour when using the Terms query: WP_Term_Query::__construct() | Method | WordPress Developer Resources

  • include int|string
    Array or comma/space-separated string of term IDs to include.
    Default empty array.

Would you be so kind as to confirm this before I move the thread to general.

Best regards,

Hi @JohnD,

unfortunately I don’t really understand these parameters so I can’t come to a conclusion myself just by looking at the developer resources.

If this indeed is intended behavior, would it be possible to modify the visibility conditions in a way to be able to hide the query in case the “default empty array” kicks in?
For example adding custom taxonomies to the visibility conditions so I could select: Show if > TaxonomyA > equal to > empty
Or something similar.

Because even if it is the default behavior it does not make it intuitive since it does not really make sense to display all terms when none are selected.

Hi @Jonas,

It makes sense in the context of a filter, in the sense that, you can show all results initially and allow a user to filter down those results by criteria.

I think it’s just two different models of thinking - one is starting with nothing and adding, and one is starting with everything and subtracting. There are valid use cases for both approaches.

To achieve what you want, you can potentially use a Visibility condition based on whether the url parameter of your filter is non-empty - that way you can hide the query if no filters have been selected. I haven’t tested this and it may cause the filters to not function if the Query is removed by the conditions, so needs to be tested.

Hi @StrangeTech,

ok, now I understand the logic behind this. Thanks for this explanation! In my case I don’t use any frontend filter though. I have a CPT where I want to display the Countries (Custom Taxonomy) that the post is assigned to. So when no country is selected the query should be empty.

As you can see in the screenshot of @Araminta I set up the query to include all Post Terms. That’s why I expect the query to be empty when no Post Terms are present.