How to Use meta_query to Compare Between Price or Two Numbers

To get the results between two numbers with the meta_query, all you have to pass your meta value in an array and use between operator to compare the value. Let’s understand by the example given below.

meta_query to compare between price or two numbers

Let’s say, I have a meta key or custom field named ‘price’, and I want to get the results which have prices from 100 to 500. So in that case, I will write meta_query like given below:


$args = array(
 
'post_type'       => 'products', //  post type name 
'posts_per_page'  => 5,
'meta_query'      => array(
                   		array( 
                 			'key'	   => 'price',
                 			'value'    =>  array(100 , 500),
                 			'type'     => 'numeric',
                 			'compare'  => 'between'
                              )
            		)
)
 
$query = new WP_Query( $args );
// Rest of the loop content goes here

This will give you the results for all the products that have prices from 100 to 500.

Now let’s take a closer look at the meta query in the below code.

'meta_query'  => array(
            array( 
                	
                	'key'      => 'price',
                	'value'    =>  array(100 , 500),
                	'type'     => 'numeric',
                	'compare'  => 'between'

                )
            )

So as you can see above:

  • I have used two numbers or prices in the array 'value' => array(100 , 500) and assign it to the meta value.
  • I have used 'type' => 'numeric' type of the values in the array and that is numeric, so it will compare the values even if they are in point eg: 120.67 or 342.58
  • I have used 'compare' => 'between' between operator to compare the price values in given array

meta_query to compare between price with multiple meta keys

Now let’s take the current example and take it to the one step further:

Example:

let’s say I have another meta key named .‘sale_price’ and now I want to get the results for all products that have .prices from 100 to 500 and also have sale price between 50 to 300.


$args = array(
 
    'post_type'       => 'products', //  post type name 
    'posts_per_page'  => 5,
    'meta_query'      => array(
    
                array( 
                    
                    	'key'     => 'price',
                    	'value'   =>  array(100 , 500),
                        'type'    => 'numeric',
                        'compare' => 'between'                                        
                    
                     ),
 
                array( 

                	    'key'     => 'sale_price',
                	    'value'   => array( 50, 300 ),
                	    'type'    => 'numeric',
                    	'compare' => 'between'                                        
                    
                    ),
 
                'relation' => 'AND'
            )
)
 
$query = new WP_Query( $args );

// Rest of the loop content goes here

This code will fetch all the products that have prices from 100 to 500 and thier sale price is between 50 to 300. Now let’s take a closer look at the meta query at the above code.


 'meta_query'      => array(
     
                array( 
                     
                        'key'     => 'price',
                        'value'   =>  array(100 , 500),
                        'type'    => 'numeric',
                        'compare' => 'between'                                       
                     
                     ),
  
                array( 
 
                        'key'     => 'sale_price',
                        'value'   => array( 50, 300 ),
                        'type'    => 'numeric',
                        'compare' => 'between'                                       
                     
                    ),
  
                'relation' => 'AND'
            )

So as you can see in the above meta query code, I have passed two arrays one for price and other one for sale price and used 'relation' => 'AND' to create a relationship between these two arrays.

I hope this article helps you to understand how to use meta query to get results between two numbers or prices for one or multiple meta keys.

Share it to the Community!

You May Also Like These meta_query Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *