On a custom page I output products from a category using wp_query. How can I output a list of attributes and their number from these products for further filtering?
Size (pa_size) (radio or checkboxs)
-S (2)
-M (1)
Color (pa_color) (radio or checkboxs)
-Red (15)
-Blue (4)
...
wp_query products:
$query = new WP_Query($args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => 41,
),
),
));
?>
<div class="products">
<?php
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
wc_get_template_part('content', 'product-list');
endwhile;
wp_reset_postdata();
endif;
?>
</div>
For example get_terms("pa_size"); shows all attributes. WC Filters on custom page do not work. I know how to filter further by attributes, but I don’t know how to get a list of attributes from these products.
You can use the following custom utilities function to get product attributes from a WP_Query and display radio buttons for each product attribute terms:
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
USAGE: Then you can use it in your code like:
You will get something like: