WooCommerce似乎只订购日期而不是价格 [英] WooCommerce seems to only orderby date and not price

查看:52
本文介绍了WooCommerce似乎只订购日期而不是价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过自定义WP_Query加载可变产品

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

这似乎很好.但是,我正在使用ajax重新加载产品,但使用了诸如此类的另一种循环.

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

但是我注意到,在"asc"和"desc"之间,顺序被翻转了,所以至少这是可行的.我的问题是orderby值似乎没有什么区别.如何使循环更改是否按日期或价格订购产品?

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?

谢谢!

推荐答案

尝试一下:

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

这篇关于WooCommerce似乎只订购日期而不是价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆