0

I have different taxonomies, let's say category and colour, these 2 tax have different terms, the tax are in parallel, a product can be in the category A and have colours X, in my frontend when I go in the page for the category A I show all the products in the category A, in this page I also show all the colours as filter, I'd like to hide all the colour that are not present in any product in the category A.

Here an example that should help understand my issue:

Product 1:

  • category: car
  • color: red

Product 2:

  • category: car
  • color: black

Product 3:

  • category: bike
  • color: purple

if I go in the page of category "car" and I do something like:

get_terms('colours', array( 'hide_empty' => true )) 

I'm able to see purple, even if in this category there are no product with colour purple, that make sense because WP check the total count for that particular taxonomy but I would like to find a way to get the colour only for the products in the category A

so:

get_terms('colours', array( 'hide_empty' => true )) 

should returns only ["red", "black"]

many thanks

4
  • 1
    Purple is not empty as Product 3 is purple, get_terms will not change its results based on context, the only context it knows is the array of parameters you passed, so it will always return the same values regardless of the current category you are in. The problem here is terminology, you do not want to hide empty terms, you want to hide terms that do not have products from the current page/query, and those are not the same thing. That's why hide_empty does not do what you expected Commented Mar 1, 2022 at 15:15
  • 1
    yes you are right, I used the wrong terms but the reason why I did that is because I want to replicate the function of hide_empty based on the current list of products. Commented Mar 1, 2022 at 15:31
  • The solution would be to run a foreach check after the terms have been loaded and then check whether or not the term is assigned to that particular product. Commented Mar 12, 2022 at 14:37
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Commented Mar 12, 2022 at 14:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.