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);
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);
Share
Improve this question
asked Sep 28, 2016 at 17:03
AppleTattooGuyAppleTattooGuy
1051 silver badge4 bronze badges
4
- You are going to check the amount less than 25000 or what it seems – Naresh Kumar P Commented Sep 28, 2016 at 17:05
- where is $amount and $months coming from? – Ahmed Fouad Commented Sep 28, 2016 at 17:07
- @AhmedMahdi coming from a POST variable, this is part of an AJAX call – AppleTattooGuy Commented Sep 28, 2016 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 Commented Sep 28, 2016 at 17:12
3 Answers
Reset to default 1In 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>=
<=
- Less than or Equal to>=
- 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 AND / OR depending on your choice
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.
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' => '>='
)
));
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'
)
));