Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce seems to only orderby date and not price

I am loading in variable products via a custom WP_Query

$args = array(
    'post_type' => 'product',
    'posts_per_page' => 100,
    'product_cat' => 'beast-balls',
    'orderby' => 'date',
    'order' => 'desc'
    );
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <div class="product-node cat-beast-balls">
        <?php wc_get_template_part( 'content', 'single-product' ); ?>
        </div>
    <?php endwhile;
}

wp_reset_postdata();

This seems to work fine. However, I am using ajax to reload the products but with a different loop such as this one.

$args = array(
    'post_type' => 'product',
    'posts_per_page' => 100,
    'product_cat' => 'beast-balls',
    'orderby' => 'price',
    'order' => 'asc'
    );
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <div class="product-node cat-beast-balls">
        <?php wc_get_template_part( 'content', 'single-product' ); ?>
        </div>
    <?php endwhile;
}

wp_reset_postdata();

I can notice however that between 'asc' and 'desc' the order is flipped, so at least that's working. My problem is that the orderby value seems to make no difference. How can I make it so that the loop changes whether or not the products are ordered by date or price?

Thanks all!

like image 970
Mikey Musch Avatar asked Jun 22 '15 11:06

Mikey Musch


1 Answers

Try this:

$args = array(
    'post_type' => 'product',
    'posts_per_page' => 100,
    'product_cat' => 'beast-balls',
    'orderby'   => 'meta_value_num',
    'meta_key'  => '_price',
    'order' => 'asc'
    );
like image 83
Rohil_PHPBeginner Avatar answered Nov 14 '22 21:11

Rohil_PHPBeginner