I'm trying to write a wordpress custom query ( WP_Query
) which filters posts by rating limit. This rating is a number between 0 and 10 and it maybe floating numbers as well ( 6.8 for example ) , i tried this code but it doesn't work :(
我正在尝试编写一个wordpress自定义查询(WP_Query),它按评级限制过滤帖子。此评级是介于0和10之间的数字,也可能是浮动数字(例如6.8),我尝试了此代码但它不起作用:(
<?php
$ratings = array( 4, 7 ); // this is an example , ratings are dynamic
$args = array(
'post_type' => 'product',
'showposts' => -1,
'meta_query' => array(
array(
'key' => 'aps-product-rating-total', // floating number
'value' => $ratings,
'type' => 'DECIMAL',
'compare' => 'BETWEEN'
)
);
);
$filter_result = new WP_Query( $args );
?>
1 个解决方案
#1
The answer is simple - remove the 'type' attribute and the search will respect your decimals. I learned this by outputting the query as suggested by m.cichacz. You can see that there is typecasting on the fields when either NUMERIC or DECIMAL is specified, but for some reason this is causes the query to ignore anything after the decimal place. Remove the typecasting and it works.
答案很简单 - 删除'type'属性,搜索将尊重您的小数。我通过输出m.cichacz建议的查询来了解这一点。当指定NUMERIC或DECIMAL时,您可以看到字段上存在类型转换,但由于某种原因,这会导致查询忽略小数点后的任何内容。删除类型转换,它的工作原理。
#1
The answer is simple - remove the 'type' attribute and the search will respect your decimals. I learned this by outputting the query as suggested by m.cichacz. You can see that there is typecasting on the fields when either NUMERIC or DECIMAL is specified, but for some reason this is causes the query to ignore anything after the decimal place. Remove the typecasting and it works.
答案很简单 - 删除'type'属性,搜索将尊重您的小数。我通过输出m.cichacz建议的查询来了解这一点。当指定NUMERIC或DECIMAL时,您可以看到字段上存在类型转换,但由于某种原因,这会导致查询忽略小数点后的任何内容。删除类型转换,它的工作原理。