Mikey Musch
Mikey Musch

Reputation: 609

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!

Upvotes: 6

Views: 16305

Answers (1)

Rohil_PHPBeginner
Rohil_PHPBeginner

Reputation: 6080

Try this:

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

Upvotes: 17

Related Questions