WooCommerce seems to only orderby date and not price

天大地大妈咪最大 提交于 2021-02-08 13:43:28

问题


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!


回答1:


Try this:

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


来源:https://stackoverflow.com/questions/30978777/woocommerce-seems-to-only-orderby-date-and-not-price

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!