So far in this series you've learned how to access WordPress meta data, and work with the arrays in which they are returned. We don't just add custom fields to WordPress posts so we can display this information, but it also so we can sort by it.
Now that you know how to retrieve and display meta data, it's time to learn how to customize the WordPress loop to return only posts with specific meta values.
Using WP_Query to Query by Meta Field Value
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.
$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.
Other 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.
$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.
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
.
$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();
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:
$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>'; }
Just like with WP_Query
, we can combine different comparisons to further control which users our query returns. This next example combines the last query with a a query, this time of the wp_users
table itself for any users whose names are Luke, Han or Leia.
$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 series you've been introduced to WordPress meta data and learned some lessons about core PHP concepts along the way.
You've learned how to get the values from the fields that store post and user meta data and how to construct queries for posts and users based on meta values. With this knowledge you can are prepared to work with a few custom fields and on your way to using WordPress as a complex content management system.
Comments