个性化阅读
专注于IT技术分析

WP_Query不按价格元值排序

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

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


你需要对参数进行一些修改。请使用以下之一。

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

你已在应该位于外部的元查询中添加了排序依据。

赞(0)
未经允许不得转载:srcmini » WP_Query不按价格元值排序

评论 抢沙发

评论前必须登录!