[英]Orderby price issue in a WP_Query for Woocommerce products
I'm trying to sort my products by price but it doesn't work fine, it returns only 9 of my 56 products. 我正在尝试按价格对我的产品进行排序,但是效果不佳,它只返回我的56种产品中的9种。 All my products are similars, all are products with variations and attributes, so I don't know why it happends. 我所有的产品都是相似的,都是具有变化和属性的产品,所以我不知道为什么会这样。
This is my code to get products, it works fine if I don't use the "orderby". 这是我获取产品的代码,如果我不使用“ orderby”,它可以正常工作。
$args = [
"posts_per_page" => 20,
"paged"=> "1",
"tax_query" => [
[
"taxonomy" => "product_cat",
"field" => "term_id",
"terms" => "213",
"operator" => "IN",
"include_children" => false
],
[
"taxonomy" => "pa_color",
"field" => "term_id",
"terms" => [
"red"
],
"operator" => "IN"
],
"relation" => "AND"
],
"post_type" => [
"product",
"product_variation"
],
"meta_query" => [
[
"key" => "_price",
"value" => [
"20",
"30"
],
"compare" => "BETWEEN",
"type" => "NUMERIC"
],
"relation" => "AND"
],
"orderby" => "meta_value_num",
"meta_key" => "_price",
"order" => "ASC"
]
$loop = new WP_Query( $args );
1) Regarding product variations: 1)关于产品变化:
Woocommerce custom taxonomies as product categories or product tags are not enabled in product variations , but in their parent variable product . Woocommerce自定义分类法作为产品类别或产品标签 未在产品变体中启用 ,但在其父变量产品中 启用 。
Product attributes (for variations) are not available through a tax query. 产品属性 (用于变体)无法通过税收查询获得。 For product variation they are set as meta data like attribute_pa_color
(all keys starting by attribute_
) and the value is a term slug for each one 对于产品变体,它们被设置为元数据,例如attribute_pa_color
(所有键均以attribute_
开头),并且每个值的值都为词条
So you can't make your query work for both post types product
and product_variation
. 因此,您无法同时对product
类型product
和product_variation
进行查询。
Note: Product variations are not displayed by default in product loops . 注意: 默认情况下,产品循环中不会显示产品版本。 They are only displayed in cart items and order items 它们仅显示在购物车项目和订单项目中
2) Regarding Variable products: 2)关于可变产品:
_prices
) as there is variations in it. 关于可变产品的价格(自WooCommerce 3+起) ,其价格将与变化中的价格一样多(元键_prices
)。 So querying meta key _prices
can make errors, as the first available price will be taken (or may be all of them or none) . 因此,查询元键_prices
可能会出错,因为将采用第一个可用价格(或可能全部或全部不提供) 。 Note: Variable products are displayed by default in product loops, but they are never displayed in cart items and order items . 注意:可变产品默认情况下显示在产品循环中, 但从不显示在购物车项目和订购项目中 。
For all that reasons your query will not work properly anyways even with or without
"orderby" => "meta_value_num",
. 由于所有这些原因,即使有或没有"orderby" => "meta_value_num",
您的查询也无法正常工作 。
There is also some errors or mistakes in your query like: 您的查询中还存在一些错误或错误,例如:
tax_query
for "taxonomy" => "pa_color",
the "field"
need to be "slug"
instead of "term_id"
. 在您的tax_query
的"taxonomy" => "pa_color",
"field"
必须为"slug"
而不是"term_id"
。 'post_status' => 'publish',
is missing. 'post_status' => 'publish',
丢失。 "operator" => "IN"
, "relation" => "AND"
are not needed as they are default arguments. 不需要"operator" => "IN"
, "relation" => "AND"
,因为它们是默认参数。 So this doesn't solve your question, but show you that is not possible to do it on this way. 因此,这不能解决您的问题,但是向您显示不可能以这种方式进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.