我试图通过价格获取帖子query_post()
作用价格必须介于两个值之间。这是我用meta_query
. 但当我无法获得贴子时,正确的值高于999英寸\'value\' => array(0, 999)
. 当它是低所有罚款。我错在哪里?
我有价格值=100的产品。那么,在查询执行期间,1000可能被解释为100?此外,我还有价格=240的产品。当我设置“value”=>数组(02300)时,我无法获得价格为240的产品。
$args = array(
\'post_type\' => \'product\',
\'product-category\' => get_query_var(\'product-category\'),
\'orderby\' => \'wpcf-product-price\',
\'order\' => \'ASC\',
\'meta_query\' => array(
array(
\'key\' => \'wpcf-product-price\',
\'value\' => array(0, 1000),
\'compare\' => \'BETWEEN\',
),
),
);
query_posts( $args );
下面是$wp\\u查询的转储:
...["meta_query"]=>
object(WP_Meta_Query)#4450 (2) {
["queries"]=>
array(1) {
[0]=>
array(3) {
["key"]=>
string(18) "wpcf-product-price"
["value"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(10000)
}
["compare"]=>
string(7) "BETWEEN"
}
}
["relation"]=>
string(3) "AND"
}
["date_query"]=>
bool(false)
["request"]=>
string(565) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (3,6,7,8) ) AND wp_posts.post_type = \'product\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\') AND ( (wp_postmeta.meta_key = \'wpcf-product-price\' AND CAST(wp_postmeta.meta_value AS CHAR) BETWEEN \'0\' AND \'10000\') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC LIMIT 0, 10"
["posts"]=>
array(1) ...{
正如你所看到的
BETWEEN
是10000,但我得到1个产品:
...["posts"]=> array(1) {...
. 当值为9999时,我拥有所有产品:
...["posts"]=> array(5) {...
最合适的回答,由SO网友:Alexander 整理而成
我通过使用找到了解决方案type
中的参数meta_query
, 将其设置为numeric
. 默认情况下是char
1000解释为字符串。
\'meta_query\' => array(
array(
\'key\' => \'wpcf-product-price\',
\'value\' => array(0, 1000),
\'type\' => \'numeric\',
\'compare\' => \'BETWEEN\',
),
),