I have Wordpress + Woocommerce site setup with about ~2k products. Products have several different attributes like "Brand" and attribute values for "Brand" like "Samsung", "Philips" etc.
On "Brand" archive page like "Samsung Archives" I want to show menu of product categories containing products with that attribute. ("Samsung products") Others I don't want to show.
I guess one way would be to query all products in category loop, then query for attributes and depending on result show/hide this category on menu. The problem is that this method can impact performance if archive page is visited a lot and/or category tree is big.
Probably someone knows better approach for this and wants to share with it?
I have Wordpress + Woocommerce site setup with about ~2k products. Products have several different attributes like "Brand" and attribute values for "Brand" like "Samsung", "Philips" etc.
On "Brand" archive page like "Samsung Archives" I want to show menu of product categories containing products with that attribute. ("Samsung products") Others I don't want to show.
I guess one way would be to query all products in category loop, then query for attributes and depending on result show/hide this category on menu. The problem is that this method can impact performance if archive page is visited a lot and/or category tree is big.
Probably someone knows better approach for this and wants to share with it?
Share Improve this question asked Nov 21, 2020 at 22:30 John DoeJohn Doe 1 1- How is brand stored in the database? I'd guess it's just a postmeta value or a taxonomy, and you can query for posts by that. – Rup Commented Nov 22, 2020 at 1:42
1 Answer
Reset to default 0OK, if any body come across this question I managed to find better and workable solution.
Before querying all the categories on sidebar I check if this is brand archive page and if yes I use wp_get_object_terms function, pass all product ids from wp query to get only needed categories. :)
In this way I show only categories which have products that have queried brand term.