What is Meta Query | How to use it with WP_Query WordPress

Meta query, is a type of query that allows you to get the results based on the meta key and meta value. By using meta_query, you can make your primary WP_Query WordPress query to get the results by one or multiple meta keys and their values.

In WordPress core, there is a WP_Meta_Query class defined. This class handles all the queries related to metadata.

This class is also used by WP_Query, WP_User_Query and WP_Comment_Query to get and filter the results by meta key and value.

How to use meta_query with WP_Query

There are two ways to create and use meta query with WP_Query.

Method 1: To work with this method all you have to focus on the arguments given below:

  • ‘meta_key’ = Name of your meta key or custom field, this will be a string.
  • ‘meta_value’ = Value of your meta key or custom field, this will be a string. However it can be an array, only if the operators used for 'meta_compare' are ‘IN’, ‘NOT IN’, ‘BETWEEN’, or ‘NOT BETWEEN’.
  • ‘meta_type’ = Type of your meta key or custom field.
  • ‘meta_compare’ = Operator to check the value of your meta key or custom field.

Example

Let’s say, I have a custom field name “rank” that is associated with my posts. Now I want to get all the posts that have rank 1. So for that in my WP_Query, will use meta query arguments like given below:

$args = array(
		'post_type' 	 => 'post',
		'posts_per_page' => 5,
		'meta_key'  	 => 'rank',
		'meta_value' 	 => '1',
		'meta_compare'	 => '=' // default operator is (=) equals to 
	);

$query = new WP_Query ($args);
	
// Rest of the WordPress Loop codes goes here

This will get all the posts that have rank 1 from the database.

Method 2: To use this method, all you have to do is use a single argument named meta_query, this argument will contain one or multiple arrays with the following keys.

  • ‘key’ = Name of your meta key.
  • ‘value’ = Value of your meta key, this will be a string. However you can pass multiple values in an array, only if the operators used for 'compare' are ‘IN’, ‘NOT IN’, ‘BETWEEN’, or ‘NOT BETWEEN’.
  • ‘compare’ = Operator to check the value of your meta key or custom field.
  • ‘type’ = Type of your meta key or custom field

These all keys work in the same ways as the first method, only the difference is when you use them with an array, you don’t need to add meta_ as a prefix.

Example 2 : Now let’s take the same case as above, so to get all the posts that have rank 1 can be done something like:


$args = array(
		'post_type' 	 => 'post',
		'posts_per_page' => 5,
		'meta_query'  	 => array(

					array( 
						'key'  	     => 'rank',
						'value' 	 => '1',
						'compare'	 => '=' // default operator is (=) equals to 
						)
					)	
		);

$query = new WP_Query ($args);

// Rest of the WordPress Loop codes goes here

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 *