它正在检查的字段的值是“25000”,我想检查变量中的值是否小于或等于此元字段。
对于10000以上的任何数字,这在大多数情况下都可以正常工作,但如果低于此值,则无法恢复结果。
我正试图找出这里的问题所在,任何意见都将不胜感激。
代码如下:
$args = array(
\'post_type\' => \'loan-offers\',
\'meta_query\' => array(
array(
\'key\' => \'amount\',
\'value\' => $amount,
\'compare\' => \'>=\'
),
array(
\'key\' => \'time\',
\'value\' => $months,
\'compare\' => \'>=\'
)
));
$custom_query = new WP_Query($args);
最合适的回答,由SO网友:Naresh Kumar P 整理而成
为了使amount变量小于25000,您需要使用调用<=
但你已经用>=
在您的查询中。
Note: 有几个比较运算符可用
= equals
!= does not equal
> greater than
>= greater than or equal to
< less than
<= less than or equal to
但在您的查询中,您误判了查询并使用了。你必须使用
<=
相反,您使用了
>=
<=
- 小于或等于>=
- 在元查询中使用大于或等于关系是值得赞赏的,因为在不指定关系的情况下,您不应该混合给定给查询执行的参数数组$args = array(
\'post_type\' => \'loan-offers\',
\'meta_query\' => array(
relation => \'AND\', // This can be AND / OR depending on your choice
array(
\'key\' => \'amount\',
\'value\' => $amount,
\'compare\' => \'<=\'
),
array(
\'key\' => \'time\',
\'value\' => $months,
\'compare\' => \'>=\'
)
));
LIKE
和NOT LIKE
是SQL operators
它允许您添加通配符,因此您可以有一个如下所示的元查询:array(
\'key\' => \'name\',
\'value\' => \'Pat\',
\'compare\' => \'LIKE\'
)
获取给定数值范围内的帖子// the loan-offers is more than 10000 and less than 25000
$rd_args = array(
\'post_type\' => \'loan-offers\',
\'meta_query\' => array(
array(
\'key\' => \'amount\',
\'value\' => array( 10000, 25000 ),
\'type\' => \'numeric\',
\'compare\' => \'BETWEEN\'
)
)
);
$rd_query = new WP_Query( $rd_args );
您可以使用BETWEEN运算符根据需要获取输出。
SO网友:Aamer Shahzad
在内部使用关系meta_query
\'relation\' => \'OR\',
$args = array(
\'post_type\' => \'loan-offers\',
\'meta_query\' => array(
relation\' => \'OR\',
array(
\'key\' => \'amount\',
\'value\' => $amount,
\'compare\' => \'>=\'
),
array(
\'key\' => \'time\',
\'value\' => $months,
\'compare\' => \'>=\'
)
));
SO网友:AppleTattooGuy
找到问题,缺少“type”=>“NUMERIC”,如下所示:
$args = array(
\'post_type\' => \'loan-offers\',
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'amount\',
\'value\' => $amount,
\'compare\' => \'>=\',
\'type\' => \'NUMERIC\'
),
array(
\'key\' => \'time\',
\'value\' => $months,
\'compare\' => \'>=\',
\'type\' => \'NUMERIC\'
)
));