0

The value of the field it is checking is '25000' and I want to check that the value in the variable is less than or equal to this meta field.

This is working fine in the most part for any number above 10000 but anything below this it doesn't bring the results back.

I am trying to figure out what the issue is here, any input will be greatly appreciated.

The code is as follows:

$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); 
  • You are going to check the amount less than 25000 or what it seems – Naresh Kumar P Sep 28 '16 at 17:05
  • where is $amount and $months coming from? – Ahmed Fouad Sep 28 '16 at 17:07
  • @AhmedMahdi coming from a POST variable, this is part of an AJAX call – AppleTattooGuy Sep 28 '16 at 17:10
  • @NareshKumar.P Yes, the $amount variable should be less than 25000 - it works except if the amount is less than 10000 - if the input is 10000 it's fine if it's 9000 it doesnt work – AppleTattooGuy Sep 28 '16 at 17:12
0

In order to deliver the amount variable to be less than 25000 you need to check the query with the operator called <= but you have tested it with >= in your query.

Note: There are several comparison operators available

=   equals
!=  does not equal
>   greater than
>=  greater than or equal to
<   less than
<=  less than or equal to

But in your query you have misjudged the query and used. You have to use <= rather you have used >=

  1. <= - Less than or Equal to
  2. >= - Greater than or Equal to

Usage of relation is appreciated in the Meta Query since without specifying the relation you are not supposed to mix the array of parameters that is given to the query for execution

$args = array(
'post_type' => 'loan-offers',
'meta_query' => array(
        relation' => 'AND', // This can be depending on your choice ( AND / OR)
        array(
            'key' => 'amount',
            'value' => $amount,
            'compare' => '<='
        ),

        array(
            'key' => 'time',
            'value' => $months,
            'compare' => '>='
        )
));

LIKE and NOT LIKE are SQL operators that let you add in wild-card symbols, so you could have a meta query that looks like this:

array( 
    'key' => 'name', 
    'value' => 'Pat', 
    'compare' => 'LIKE'
)

Get Posts Within a Given Range of Numeric Meta Values

// 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 );

You can use BETWEEN operator for getting the output as required by you.

  • Thank you, the problem now however is if I put in a figure below 10000 or equal to 25000 it works but anything above 10000 or below 25000 it doesnt work – AppleTattooGuy Sep 28 '16 at 17:26
  • You need to set the limit to 10000 and 25000 right and you need to get the values within that – Naresh Kumar P Sep 28 '16 at 17:28
  • Found the issue, was missing the 'type' => 'NUMERIC' - thanks for your help – AppleTattooGuy Sep 28 '16 at 17:35
  • I have mentioned it right over to my answer :) But you have not seen it right:) – Naresh Kumar P Sep 28 '16 at 17:36
0

use relation inside 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' => '>='
        )
));
0

Found the issue, was missing the 'type' => 'NUMERIC' as follows:

$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'
        )
));

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.