比较整数值“>=”或“<;=”时遇到一些问题使用meta\\u查询和WP\\u USER\\u查询。
假设我已使用以下元值存储了以下元键:
meta_key meta_value
type type1
status active
location London
min_price 10000
max_price 20000
对于元键“type”、“status”和“location”,查询工作正常。但我对“最低价格”和“最高价格”有问题。
下面是我用来检索具有上述元键的用户列表的代码:
// Values
$type = \'type1\';
$status = \'active\';
$location = \'London\';
$price = 15000;
// Query args
$args = array(
\'role\' => \'end-user\',
\'orderby\' => \'ID\',
\'order\' => \'ASC\',
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'type\',
\'value\' => $type,
\'compare\' => \'=\'
),
array(
\'key\' => \'status\',
\'value\' => $status,
\'compare\' => \'=\'
),
array(
\'key\' => \'location\',
\'value\' => $location,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'min_price\',
\'value\' => $price,
\'compare\' => \'>=\',
\'type\' => \'NUMERIC\'
),
array(
\'key\' => \'max_price\',
\'value\' => $price,
\'compare\' => \'<=\',
\'type\' => \'NUMERIC\'
),
)
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query($args);
// Get the results
$users = $wp_user_query->get_results();
// Check if we have users
if (!empty($users)) {
// Loop through results
foreach ($users as $user) {
echo $user->ID;
}
} else {
echo \'No Users Found!\';
}
此查询返回“找不到用户!”。但是,如果我使用为“min\\U price”和“max\\U price”元查询存储的相同值,它就可以工作。E、 g.:
array(
\'key\' => \'min_price\',
\'value\' => 10000,
\'compare\' => \'>=\',
\'type\' => \'NUMERIC\'
),
array(
\'key\' => \'max_price\',
\'value\' => 20000,
\'compare\' => \'<=\',
\'type\' => \'NUMERIC\'
),
10000和20000以外的其他值不起作用。请帮忙。