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
get_termswill 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 whyhide_emptydoes not do what you expectedhide_emptybased on the current list of products.