我有这个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);
【WP_Query不按价格元值排序】实际上, 按其他任何属性排序都可以, 但不能正常使用价格。提前致谢。
#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);
你已在应该位于外部的元查询中添加了排序依据。
推荐阅读
- WP_Query-显示粘性帖子时,” category__not_in”不起作用
- wp_posts缺少文本()
- WP(覆盖最近发布的窗口小部件类)
- 基于post_type的边栏中的wp_list_categories
- WP(如何在functions.php中包含一个类(php文件),而不是将其记为functions.php中的(长)函数)
- wp_enqueue_scripts无法渲染我的脚本
- wp CSS,在#main中覆盖width属性(这应该很容易)
- #yyds干货盘点#Windows Server之终端服务安装
- #yyds干货盘点# Spring核心原理分析之MVC九大组件