WP_Query没有按价格元值排序

时间:2018-03-28 13:06:54

标签: wordpress wordpress-theming

我有这个wp_query $ args从woocommerce获取产品,通过我用于过滤器的一些$ _GET参数来查询它们。 实际上我的问题是按价格排序根本不起作用。我多次使用这种属性,但实际上这里并没有工作。 我在这里贴了我的代码。

if ($_GET['filter_pietre'] != -1 && $_GET['filter_pietre'] != NULL) {
    $pietre_operator = 'IN';
} else {
    $pietre_operator = 'NOT IN';
}
if ($_GET['filter_metals'] != -1 && $_GET['filter_metals'] != NULL) {
    $metals_operator = 'IN';
} else {
    $metals_operator = 'NOT IN';
}
if ($_GET['filter_finitura'] != -1 && $_GET['filter_finitura'] != NULL) {
    $finishes_operator = 'IN';
} else {
    $finishes_operator = 'NOT IN';
}
if ($_GET['filter_coloresmalto'] != -1 && $_GET['filter_coloresmalto'] != NULL) {
    $pa_coloresmalto = 'IN';
} else {
    $pa_coloresmalto = 'NOT IN';
}
if ($_GET['filter_ispirazione'] != -1 && $_GET['filter_ispirazione'] != NULL) {
    $pa_ispirazione = 'IN';
} else {
    $pa_ispirazione = 'NOT IN';
}
$params = array(
        'posts_per_page' => -1,
        'post_type' => 'product',
        'meta_query' => array(
            //filters
            'relation' => 'AND',
            array(
                'key' => '_stock_status',    
                'value' => 'instock'
            ),
            array(
                'meta_key' => '_price',
                'orderby'  => 'meta_value_num',
                'order' => 'ASC',
                'type' => 'NUMERIC'
            ),
        ),
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field' => 'id',
                'terms' => $term_id
            ),
            array(
                'taxonomy' => 'pa_pietre',
                'terms' => $_GET['filter_pietre'],
                'field' => 'slug',
                'operator' => $pietre_operator
            ),
            array(
                'taxonomy' => 'pa_metals',
                'terms' => $_GET['filter_metals'],
                'field' => 'slug',
                'operator' => $metals_operator
            ),
            array(
                'taxonomy' => 'pa_finishes',
                'terms' => $_GET['filter_finitura'],
                'field' => 'slug',
                'operator' => $finishes_operator
            ),
            array(
                'taxonomy' => 'pa_coloresmalto',
                'terms' => $_GET['filter_coloresmalto'],
                'field' => 'slug',
                'operator' => $pa_coloresmalto
            ),
            array(
                'taxonomy' => 'pa_ispirazione',
                'terms' => $_GET['filter_ispirazione'],
                'field' => 'slug',
                'operator' => $pa_ispirazione
            )
        ),
    );
$wc_query = new WP_Query($params);

实际上按任何其他属性排序可以正常工作,但不能付出代价。 提前致谢。

1 个答案:

答案 0 :(得分:2)

您需要对参数进行一些修改。请使用下面的一个。

   $params = array(
    'posts_per_page' => -1,
    'post_type' => 'product',
    'orderby'   => 'meta_value_num',
    'meta_key'  => '_price',
    'order' => 'asc',
    'meta_query' => array(
        //filters
        'relation' => 'AND',
        array(
            'key' => '_stock_status',    
            'value' => 'instock'
        )

    ),
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => $term_id
        ),
        array(
            'taxonomy' => 'pa_pietre',
            'terms' => $_GET['filter_pietre'],
            'field' => 'slug',
            'operator' => $pietre_operator
        ),
        array(
            'taxonomy' => 'pa_metals',
            'terms' => $_GET['filter_metals'],
            'field' => 'slug',
            'operator' => $metals_operator
        ),
        array(
            'taxonomy' => 'pa_finishes',
            'terms' => $_GET['filter_finitura'],
            'field' => 'slug',
            'operator' => $finishes_operator
        ),
        array(
            'taxonomy' => 'pa_coloresmalto',
            'terms' => $_GET['filter_coloresmalto'],
            'field' => 'slug',
            'operator' => $pa_coloresmalto
        ),
        array(
            'taxonomy' => 'pa_ispirazione',
            'terms' => $_GET['filter_ispirazione'],
            'field' => 'slug',
            'operator' => $pa_ispirazione
        )
    ),
);
 $wc_query = new WP_Query($params);

您已在元查询中添加了应位于外部的订单。