问题描述
我正在尝试以几乎相似的正常价格展示 woocommerce 产品.例如:当前产品的正常价格为 1000 美元
I am trying to display the woocommerce products with almost similar regular price.For eg: Regular price of the current product is $1000
和其他一些产品的价格是
and some other products with prices are
- product1 - 1001 美元
- product2 - 1006 美元
- product3 - 996 美元
- product4 - 999 美元
- product5 - 1003 美元
- product6 - 1001 美元
- product7 - 1005 美元
- product8 - 1010 美元
- product9 - 998 美元
- product10 - 990 美元
现在,如果我想按价格显示 4 个最接近的产品,那么它们将是产品 1、6、4、9
Now if I was suppose to show 4 nearest products by price then they would be product 1,6,4,9
因为上面对我来说似乎有点困难所以我试图得到
Because above seemed a bit difficult to me so I tried to get
- 价格超过 1000 美元的两款产品和
- 两种价格超过 1000 美元的产品
这是我尝试过的 wp 查询参数
Here are the arguments for wp query that I tried
$args = array(
'post_type'=>'product',
'posts_per_page'=>2,
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => '_price',
'meta_query' => array(
array(
'key' => '_price',
'value' => 1000,
'compare' => '<',
'type' => 'NUMERIC'
),
)
);
我想它应该给我 2 个最近且价格低于 1000 的产品
I guess it should give me 2 nearest products with prices less 1000
但问题是它也给了我没有定价的产品..所以我想排除没有定价的产品..
But issue is it also gives me products for which the price is not set ..So I want to exclude the products with no price set..
提前致谢
推荐答案
您可以使用多个元查询,因此您可以添加一个来检查价格是否高于 0:
You can use multiple meta queries, so you could add one to check if the price is higher then 0:
$args = array(
'post_type'=>'product',
'posts_per_page'=>2,
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => '_price',
'meta_query' => array(
array(
'key' => '_price',
'value' => 1000,
'compare' => '<',
'type' => 'NUMERIC'
),
array(
'key' => '_price',
'value' => 0,
'compare' => '>',
'type' => 'NUMERIC'
)
)
);
您不需要设置关系参数,因为它默认设置为AND".
You do not need to set the relation parameter, as it is set to "AND" by default.
这篇关于排除没有价格设置的 woocommerce 产品 - wp 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!