在自定义wp_query循环中按价格订购产品

时间:2016-08-29 11:46:28

标签: php wordpress woocommerce

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

$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' );
}

这是我想要的,除了我无法按产品价格(升序或降序)订购产品 - 我需要做些什么来使这项工作?

1 个答案:

答案 0 :(得分:2)

试试这个:

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

希望它会对你有所帮助。