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 with single or multiple meta keys and values.
How to use meta_query with single meta key | value
To get the results using wp_query with single meta key and value, you will have to pass some additional arguments inside the array of arguments for wp_query. meta query arguments are 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. By defaults it checks for equals to.
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 equals to 1.
So to do that, I will pass meta query arguments inside the array of arguments for wp_query:
$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
So as you can see in the code above, I have used a single 'meta_key' => 'rank'
and it’s value 'meta_value' => '1'
along with the equals operator for compare 'meta_compare' => '='
to get the results with wp_query.
The above code will get all the posts that have the meta key ‘rank’ and it’s value equals 1.
How to Use meta_query with multiple meta keys | values
Let’s take the use of meta query with WP_Query one step further. In this step, you will learn how you can use WP_Query with multiple meta keys and values to get the results.
Now using multiple meta keys with their values along with WP_Query is little bit different then the single one.
In this case, all you have to do is use a single argument named meta_query inside the array of arguments for WP_Query.
This meta_query argument will contain one or multiple arrays with the following arguments.
'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 arguments work in the same way as they work for a single meta key or value, only the difference is when you use them for multiple meta keys and values, you don’t need to add meta_ as a prefix.
Example:
Let’s say, I have two meta keys named “rank” and “post_score” that are associated with the posts. Now I want to get all the posts that have rank less than 3 and post_score is greater than 80.
So to do that, I will pass meta_query argument inside the array of WP_Query arguments and pass the array of meta keys and values to it. It seems quite confusing?… Let’s see the code below.
$args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => '5', 'meta_query' => array( array( 'key' => 'rank', 'value' => '3', 'compare' => '<' ), array( 'key' => 'post_score', 'value' => '80', 'compare' => '>' ) ) ); $query = new WP_Query($args); // Rest of the WordPress Loop codes goes here
The above code will get the all the posts which have rank less than 3 and post score greater than 80. You can use multiple meta keys by just passing them as an array element inside the meta_query array.
How to use meta_query with custom post type
To use meta query with custom post type, you just need to change the value of 'post_type' => 'your_custom_post_Type'
. And along with this you can use meta_query arguments as per your meta keys and values.
Example
You can check the code snippet given below to take a reference or understand the use of meta_query with custom post type.
$args = array( 'post_type' => 'movies', // "movies" is custom post type here 'posts_per_page' => '5', 'meta_key' => 'rating', 'meta_value' => '4', 'meta_compare' => '<' // default operator is (=) equals to ); $query = new WP_Query ($args); // Rest of the WordPress Loop codes goes here
I hope this article helps you to understand what is meta_query and how to use it with wp_query along with single and multiple meta keys and values.