Table of Contents
Mastering WordPress Meta Data: Querying Posts and Users with Meta Data
When working with WordPress, it’s essential to understand how to query posts and users based on their meta data. By utilizing WP_Query and WP_User_Query, you can customize the results to meet specific requirements. In this article, we will explore these techniques in detail, providing professional explanations along with practical examples.
1. Querying Posts by Meta Field Value using WP_Query
In order to customize what posts WordPress returns based on meta fields, we need to use WP_Query
and specify a meta_query
. If, for example, we had a custom post type called ‘films’ that had a custom field called ‘director’ we could query for films whose director was a director of one of the three Star Wars films.
Take a look at the code below and see if you can use your mastery of arrays that you gained in the last part to understand what is going on with the meta_query
, which I will break down what is going on under the code.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
|
$sw_args = array ( 'post_type' => 'films' , 'meta_query' => array ( array ( 'key' => 'director' , 'value' => array ( 'George Lucas' , 'Richard Marquand' , 'Irvin Kershner' ), 'compare' => 'IN' , ) ) ); $query = new WP_Query( $sw_args ); if ( $the_query ->have_posts() ) { echo '<h2>Films By Star Wards Directors</h2>' ; echo '<ul>' ; while ( $the_query ->have_posts() ) { $the_query ->the_post(); echo '<li>' . get_the_title() . '</li>' ; } echo '</ul>' ; } /* Restore original Post Data */ wp_reset_postdata(); |
As you can see we have an array of director’s names nestled inside of three other arrays. Let’s take it apart, piece by piece.
First, we start an array for our WP_Query
args. After our first argument ‘post_type’, we begin an array to house our meta_query
arguments.
Inside of that we specify which key to look for–in the case ‘director’. We also provide an array of values to search for in that key.
The last argument is how to compare those values, in this case we specify “IN” to retrieve any posts with these values in the key director.
2. Additional Comparisons
What if we wanted films that where directed by a director of a Star Wars film, but wan’t to exclude the Star Wars Prequels? We can just add another array of arguments to our meta_query
, but this time for value use an array of those films titles for the key film_title
and for compare, use ‘NOT LIKE’ to exclude post that have these values in the film_title
field.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
$sw_args = array ( 'post_type' => 'films' , 'meta_query' => array ( array ( 'key' => 'director' , 'value' => array ( 'George Lucas' , 'Richard Marquand' , 'Irvin Kershner' ), 'compare' => 'IN' , ), array ( 'key' => 'film_title' , 'value' => 'Phantom Menace' , 'Attack of the Clones' , 'Revenge of the Sith' ), 'compare' => 'NOT LIKE' ), ) ); $query = new WP_Query( $sw_args ); |
Now WordPress will look for films by these three directors, whose title is not one of the three prequels.
3. Displaying Meta Fields in WP_Query
So far I’ve shown you how to use WP_Query
to find posts that have specific values for a custom field, but not how to show those fields.
Showing these fields is pretty much the same as before, but instead of using get_the_ID()
to specify ID for get_post_meta()
we specify it in an object context. So in our loop, which you can see below, the ID is retrieved a little differently using $query->post->ID
.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
$sw_args = array ( 'post_type' => 'films' , 'meta_query' => array ( array ( 'key' => 'director' , 'value' => array ( 'George Lucas' , 'Richard Marquand' , 'Irvin Kershner' ), 'compare' => 'IN' , ), array ( 'key' => 'film_title' , 'value' => 'Phantom Menace' , 'Attack of the Clones' , 'Revenge of the Sith' ), 'compare' => 'NOT LIKE' ), ) ); $query = new WP_Query( $sw_args ); if ( $the_query ->have_posts() ) { echo '<h2>Films By Star Wards Directors</h2>' ; echo '<ul>' ; while ( $the_query ->have_posts() ) { $the_query ->the_post(); echo '<li><ul>' ; echo '<li>' . get_the_title() . '</li>' ; echo '<li>' . get_post_meta( $query ->post->ID, 'director' , true ). '</li>' ; echo '</ul></li>' ' } echo '</ul>' ; } /* Restore original Post Data */ wp_reset_postdata(); |
4. Querying Users by Meta Data using WP_User_Query
Just like in the last part when we used WP_Query
to find posts with certain values for various custom fields, we can use WP_Query
‘s user table equivalent WP_User_Query
.
For example, if we had a custom field called subscriber_level
and wanted to find only those users who had the subscriber level of extra_special
or super_special
we could, much the same way we searched for films directed by the three directors of the Star Wars films:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
$args = array ( 'meta_query' => array ( array ( 'key' => 'subscriber_level' , 'value' => array ( 'extra_special' , 'super_special' ); 'compare' => '=' ) ) ); $user_query = new WP_User_Query( $args ); if ( ! empty ( $user_query ->results ) ) { echo '<h3>Extra and Super Special Users</h3>' ; echo '<ul>' ; foreach ( $user_query ->results as $user ) { echo '<li>' . $user ->display_name . '</li>' ; } echo '</ul>' ; } |
5. Combining Different Queries
To further refine our user query, we can combine multiple comparisons and search for users with specific names alongside the desired subscriber levels. For example, let’s search for users named ‘Luke,’ ‘Han,’ or ‘Leia’ who also have the subscriber levels ‘extra_special’ or ‘super_special’:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
|
$args = array ( 'meta_query' => array ( array ( 'key' => 'subscriber_level' , 'value' => array ( 'extra_special' , 'super_special' ); 'compare' => '=' ) ), 'search' => array ( 'Luke' , 'Han' , 'Leia' ), 'search_columns' => array ( 'user_nicename' , 'display_name' ), ); $user_query = new WP_User_Query( $args ); if ( ! empty ( $user_query ->results ) ) { echo '<h3>Extra and Super Special Users Named Luke, Han or Leia</h3>' ; echo '<ul>' ; foreach ( $user_query ->results as $user ) { echo '<li>' . $user ->display_name . '</li>' ; } echo '</ul>' ; } |
Wrapping Up
In this article, we explored the process of querying posts and users by their meta data in WordPress. By mastering WP_Query and WP_User_Query, you can tailor your queries to retrieve specific results based on meta field values. The provided examples demonstrate how to construct and execute these queries effectively, empowering you to utilize WordPress as a powerful content management system.