1. Code
  2. WordPress

Mastering WordPress Meta Data: Querying Posts and Users by Meta Data


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.

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.

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.

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:

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.

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.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.