C. Wood
C. Wood

Reputation: 33

Ordering products by price in custom wp_query loop

I currently have a very simple wp_query loop to loop through my WooCommerce products like so:

$args = array(
    'posts_per_page' => -1,
    'product_cat' => $cat,
    'post_type' => 'product',
    'orderby' =>  'price',
    'order' => 'DESC'
);

$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) {
   $the_query->the_post();
     wc_get_template_part( 'content', 'product' );
}

This works as I want it to, except I can't get it to order the products by product price (ascending or descending) - what do I need to do to make this work?

Upvotes: 3

Views: 12344

Answers (1)

Noman
Noman

Reputation: 1487

Try this:

$args = array(
    'posts_per_page' => -1,
    'product_cat' => $cat,
    'post_type' => 'product',
    'orderby' => 'meta_value_num',
    'meta_key' => '_price',
    'order' => 'asc'
);

Hope it will help you.

Upvotes: 9

Related Questions