Support » Plugin: WooCommerce » Woocommerce: sorting products by their category in a foreach loop

  • Resolved adzetko


    I’m doing a custom product-archive.php as my main shop page, so, in my child theme I have this custom product loop:

    if ( wc_get_loop_prop( 'total' ) ) {
        $categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
        foreach ( $categories as $category ) {
            $title_value = (mb_strlen($category->parent) + 1);
            while ( have_posts() ) {
                 * Hook: woocommerce_shop_loop.
                do_action( 'woocommerce_shop_loop' );
                wc_get_template_part( 'content', 'product' );

    The problem is that actually, it displays all the products available on the shop, for each category, because I don’t know how to tell the product-fetching query to only show products from the actual category. How can I tell it to do so?

    • This topic was modified 1 year, 2 months ago by adzetko.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support thup90


    Hi there,

    You can use the conditional tag is_product_category() to get only products from a category:

    Hari Shanker R


    Automattic Happiness Engineer

    Hi @adzetko

    Just wanted to give you some additional inputs on your question.

    I looked at the code you have shared.

    I can see that you have trie to get a category list with:

     $categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
        foreach ( $categories as $category ) {

    However, the code to display the posts is as follows:

      while ( have_posts() ) {

    I can see that you got this from here:

    This returns all products when used in a custom template. In order to display the category list, you will need to call a new WP_Query() with the $category variable.

    This explains how you can generate a WP_Query() for a category variable:

    You can use this WP_Query to generate products instead of using: while ( have_posts() ) { the_post(); alone (which generates all products).

    For example, if you use the variable $loop to generate a WP_Query() that has the products belonging to the category (as mentioned in stackoverflow article I shared above), you could use the following code to list the posts belonging to that category alone:

            if ( $loop->have_posts() ) {
                while ( $loop->have_posts() ) : $loop->the_post();
                    wc_get_template_part( 'content', 'product' );
            } else {
                echo __( 'No products found' );

    I hope that makes sense.

    Let us know if you have other questions. We’re happy to help!

    Plugin Support Rynald0s


    Automattic Happiness Engineer


    We haven’t heard back from you in a while, so I’m going to go ahead and mark this thread as resolved. If you have any other questions please start a new thread.


Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce: sorting products by their category in a foreach loop’ is closed to new replies.