WordPress meta data

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.

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.

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.

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.

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:

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’:

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.