Woocommere订单价格循环问题

时间:2015-11-29 06:29:38

标签: php wordpress woocommerce

我正在尝试使用简单的短代码来根据价格(降序)循环产品。由于某种原因,下面的代码没有按价格排序,它只是忽略了order属性。代码如下:

function sort_by_price( $atts ){

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 20,
        'orderby' => 'price',
        'order' => 'desc'
        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
        echo __( 'No products found' );
    }
    wp_reset_postdata();
}
add_shortcode( 'sorter', 'sort_by_price' );

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

价格不是WP_Query sortby参数的公认值。

尝试以下args:

$args = array(
        'post_type' => 'product',
        'posts_per_page' => 20,
        'meta_key' => '_price',
        'orderby' => 'meta_value_num',
        'order' => 'desc'
        );
相关问题