In WooCommerce, I'm trying to get just the sub-categories of a specific category I've created, called collections and display them in a WP_Query
however I can't seem to get just the categories it's marked to show in. I can successfully return all of the categories, just not the specific category it's set to show in.
What I've tried
I've tried using get_terms
in conjunction with product_cat
taxonomy to no avail and get_categories
too. I get all of the categories returned, rather than just the ones the product are marked to show in. I've tried pretty much every other method of getting the categories but still to no avail.
The WP_Query
<ul class="product-slider">
<?php
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'product_cat' => 'sustainable',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12'
);
$sustain = new WP_Query($args);
if ($sustain->have_posts()) : while ($sustain->have_posts()) : $sustain->the_post();
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'product-slider-img' );
?>
<div class="single-product">
<div class="test">
<div class="product__image">
<img src="<?php echo $image[0]; ?>">
</div>
<div class="product__meta">
<div class="product__name">
<?php
echo wc_get_product_category_list($product->get_id()) ?>
?>
</div>
<div class="product__subtitle">
<?php echo $product->name; ?>
</div>
<div class="product__price">
£<?php echo $product->price; ?>
</div>
</div>
<a class="product__link" href="<?php the_permalink(); ?>"></a>
</div>
</div>
<?php
endwhile; endif;
wp_reset_query();
?>
</ul>
product_cat
is a custom taxonomy, and you can't just replace the normal category
parameter with the name of the custom taxonomy. Have a look at the Codex and you'll see that there is a whole tax_query
parameter for this use.
In your example, you should replace 'product_cat' => 'sustainable',
with something like this:
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
),
),
Note: this is the theory, I can't provide a tested example as I'm not at my desk right now.