0

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.

  • Just a quick question. In your code you use the same $price as min and max value? – websupporter Oct 10 '16 at 7:59
  • Yes the $price is use to verify if it is '>=' min_price and '<=' max_price. – Nas Atchia Oct 10 '16 at 8:00
  • So you want all users >= 100 && <= 100, which would equal = 100, right? – websupporter Oct 10 '16 at 8:02
  • If a user has 10000 as value for 'min_price' and 20000 as value for 'max_price' while $price = 15000, then I want all users where '15000 >= 10000' and '15000 <= 20000' – Nas Atchia Oct 10 '16 at 8:09
0

Actually, I think your code says

Where min_price >= $price and max_price <= $price.

Try to switch the operands, like here:

    array(
        'key' => 'min_price',
        'value' => $price,
        'compare' => '<=',
        'type' => 'NUMERIC'
    ),
    array(
        'key' => 'max_price',
        'value' => $price,
        'compare' => '>=',
        'type' => 'NUMERIC'
    ),

It works for 10.000 and 20.000 since you use the bigger/equal and lower/equal operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.