How I can sort woocommerce all products by category

I have wp_query request for give all products, and needs to sort this for 2 fields:
by category & by menu_order.
Various! I need to sort by “menu_order” in each category.

In simple query:

Read More
$args = array(
        'orderby' =. 'product_cat menu_order'
        'posts_per_page' => -1,
        'post_type' => 'product',
        );

      $loop = new WP_Query($args);

In global $product, exist field “menu_order”, but not exist field “product_cat”.

Can I do it with wp_query? Or maybe exist another way to do it?

Related posts

2 comments

  1. I was founded right way to do it, code bellow, this example for

    /* Products Loop */
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                'posts_per_page' => $prod_per_page,
                'paged' => $paged,
                'post_type' => 'product',
                'product_cat' => $product_cat,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id',
                        'terms' => $subcats_ar,
                        'operator' => 'NOT IN'
                    ),
                ),
                'orderby' => array('menu_order' => 'ASC', 'title' => 'ASC')
            );
            $loop = new WP_Query($args);
    

    More simple way, without pagination and exclude sub categories:

     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                'posts_per_page' => -1,
                'post_type' => 'product',
                'product_cat' => $product_cat,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id'
                    ),
                ),
                'orderby' => array('menu_order' => 'ASC', 'title' => 'ASC')
            );
            $loop = new WP_Query($args);
    

    Magic line for this is:

    'orderby' => array('menu_order' => 'ASC', 'title' => 'ASC')
    
  2. While trying out and search i found a solution to it to use two queries to achieve this purpose the below example might help you achieve your purpose.

    <?php

    $catargs = array(

    'orderby'                  => 'name',
    
    'order'                    => 'ASC',
    

    );

    $categories = get_categories( $catargs );

    foreach ($categories as $category) {?>

    <h3><?php echo $category->name;
    // Category title ?></h3>
    <?php

    // WP_Query arguments
    $args = array (
    
        'post_type'              => 'resources',
    
        'cat'                    => $category->cat_ID,
    
        'posts_per_page'         => '-1',
    
        'order'                  => 'ASC',
    
    'orderby'                => 'title',
    
    );
    
    // The Query
    $query = new WP_Query( $args );
    

    // The Loop

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            ?>
            <h5><?php the_title(); // Post title ?></h5>
            <?php 
            // You can all phone/ email here
        }
    } else {
        // no posts found
    }
    
    // Restore original Post Data
    wp_reset_postdata();
    

    }
    ?>

    Let me know if the method worked for you.

Comments are closed.