How can I sort woocommerce all products by category

I have a wp_query to provide all products and you need to sort it for two fields: by category and by menu. Different! I need to sort by "menu_order" in each category.

In a simple query:

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

      $loop = new WP_Query($args);

      

Global $ product has a "menu_order" field, but no "product_cat" field.

Can I do this with wp_query? Or maybe there is another way to do it?

+3


source to share


2 answers


I was created correctly to do it, below is the code below, for example

/* 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);

      

Easier way, no pagination and exclude subcategories:



 $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);

      

The magic line for this:

'orderby' => array('menu_order' => 'ASC', 'title' => 'ASC')

      

+1


source


While trying to search and search, I found a solution to use two queries to achieve this goal, the example below can help you achieve your goal.

<?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 this method worked for you.

-1


source







All Articles