1

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.