在自定义 wp_query 循环中按价格订购产品 [英] Ordering products by price in custom wp_query loop

查看:22
本文介绍了在自定义 wp_query 循环中按价格订购产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个非常简单的 wp_query 循环来循环我的 WooCommerce 产品,如下所示:

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?

推荐答案

试试这个:

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

希望能帮到你.

这篇关于在自定义 wp_query 循环中按价格订购产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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