最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Using meta_query in a WP_Query not working for numbers properly

programmeradmin0浏览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); 

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
Add a comment  | 

3 Answers 3

Reset to default 1

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 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'
        )
));
发布评论

评论列表(0)

  1. 暂无评论