Reputation: 1064
I have this wp_query $args to get products from woocommerce querying them by some $_GET parameters I used for filters. Actually my problem is that sorting by price isn't working at all. I used this kind of attributes many times but actually here isn't working. I paste here my code.
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);
Actually sorting by any other attributes works properly but not price. Thanks in advance.
Upvotes: 0
Views: 1946
Reputation: 2011
You need some modification in your arguments. Please use below one.
$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);
You have added the order by in meta query that should be in outside.
Upvotes: 3