I’m having some problems when comparing integer values ‘>=’ or ‘<=’ with meta_queries with the WP_USER_QUERY.
Let say I’ve stored the following meta keys with the meta values below:
meta_key meta_value
type type1
status active
location London
min_price 10000
max_price 20000
The query works fine for the meta keys ‘type’, ‘status’ and ‘location’. But I’m having problems with the ‘min_price’ and ‘max_price’.
Below you can find the code I used to retrieve a list of users with the above meta keys:
// 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!';
}
This query returns me ‘No Users Found!’. However if I use the same values stored for the ‘min_price’ and ‘max_price’ meta query, it works. E.g.:
array(
'key' => 'min_price',
'value' => 10000,
'compare' => '>=',
'type' => 'NUMERIC'
),
array(
'key' => 'max_price',
'value' => 20000,
'compare' => '<=',
'type' => 'NUMERIC'
),
Other values than 10000 and 20000 don’t work. Any help please.