0

I have WordPress query arguments that need to filter posts based on two meta key's values based on if the two values are >= and <= to two numbers.

$args = array(
    'post_type' => 'vacancy',
    'post_status' => 'publish'
);

$vacancy_small_salary = 3000; //these are dynamically inputted from $_POST
$vacancy_large_salary = 5000; //these are dynamically inputted from $_POST

//append search queries to arguments
if ( !is_null($vacancy_small_salary) && !is_null($vacancy_large_salary) )
    $args['meta_query'] = array(
    'relation' => 'AND',
        array(
            'key'  => 'vacancy_small_salary',
            'value' => $vacancy_small_salary,
            'operator'  => '>=',
            'type' => 'NUMERIC'
        ),
        array(
            'key'  => 'vacancy_large_salary',
            'value' => $vacancy_large_salary,
            'operator'  => '<=',
            'type' => 'NUMERIC'
        )
);

$wp_query = new WP_Query( $args );

The problem I'm getting is that it won't find posts that have a $vacancy_small_salary greater than the inputted var, and $vacnacy_large_salary smaller than it's inputted var.

It does however find posts when the two values equal each other (i.e. if a post has 3000 as the small salary and 5000 as the large salary.)

Could it be that my logic is wrong with the way it manages >= and <=, or maybe the values aren't truly numeric so it cannot search properly? <- I suspect this is not the case, but it's been racking my head for a while now.

As always if you require any more information please ask. Thank you.

1 Answer 1

1

Looks like I found the solution.

I needed to use compare instead of operator in the meta_query

so

array(
  'key'  => 'vacancy_small_salary',
  'value' => $vacancy_small_salary,
  'operator'  => '>=',
  'type' => 'NUMERIC'
),

becomes

    array(
      'key'  => 'vacancy_small_salary',
      'value' => $vacancy_small_salary,
      'compare'  => '>=',
      'type' => 'NUMERIC'
    ),

and the same for the other query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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