How To Use WordPress Meta Queries Effectively

One of the things that “outside developers” as I like to call them (developers who dislike WordPress), think WordPress struggles with is retrieving data in a structured manner, I have used quite a few different frameworks and content management systems and I disagree. WordPress has a great set of functions for retrieving data, dependant on meta values, categories, tags and user data. You just have to know what the functions are and how to use them correctly.

Please note: In this example I have used get_posts as it uses WP_Query, but for multiple loops it is recommended to use WP_Query, as mentioned here in the WordPress Codex: http://codex.wordpress.org/Template_Tags/get_posts

Today i’m going to run through the Meta Query. It’s incredibly effective at retrieving data from certain areas of your site and return a nice structured object (or array) for you to loop through and output something pretty. Perfect that fits most use cases!

Is A Meta Query Useful For Me?

I would first like to mention that sometimes you might be tempted to use a meta query, but actually you don’t need to. You can retrieve a whole lot of different data just using get_posts and supplying the standard arguments, like post_type, post_status or even category.

You will want to use Meta Queries when you want to retrieve a list of posts restricted by the meta data that they contain, you might for example have a custom field called “featured_post” and that field has a yes/no select dropdown. This is perfect for a meta query.

Let’s take a look at some code and then pick it apart to find out how we can make use of Meta Queries effectively.

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'meta_key' => 'featured_post',
    'meta_value' => 'yes',
);

$posts = get_posts($args);

var_dump($posts);

The code above will retrieve the first five posts with the field featured_post set to yes. This is perfect for the front end of our site, we might wish to set a group of featured posts for the front, grab them using get_posts and allow the user to set these fields with a dropdown box.

Simple WordPress Relational Meta Queries

Next we are going to look at relational meta queries to see how we get posts if it’s set to something OR something AND something, this get’s a little more tricky, but it’s still easy to follow. First let’s take a look at some code.

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'meta_key' => 'featured_post',
            'meta_value' => 'yes',
            'compare' => "=",
        ),
        array(
            'meta_key' => 'featured_page',
            'meta_value' => 'homepage',
            'compare' => "=",
        ),
    ),
);

$posts = get_posts($args);

var_dump($posts);

In the query above we are checking to see if the featured_post field is set to yes and if the featured_page is set to homepage. This is useful because we can let the user set featured posts for different pages. So we give them a dropdown for featured posts and let them set where that featured post sits on the site, in this case we are pulling posts into the homepage, so we want to make sure they belong on the homepage.

The reason I like to work this way is because it makes it easier for the user to toggle if it is a featured post and then say where that post is featured, this makes it very easy for them to manage content, as making something a featured post is the same every time, regardless of where it sits.

WordPress Relational Meta Queries Using Compare

Next we are looking at a relational meta query using the compare parameter. This allows us to select just as we did above, but only if something DOES not equal something else. The compares you can use are, ‘=’ ‘!=’ ‘LIKE’ and ‘IN’, when you use ‘IN’ you can specify an Array to the meta_value, this will then check against each value in the array. Here is the code.

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'meta_key' => 'featured_post',
            'meta_value' => 'yes',
            'compare' => "=",
        ),
        array(
            'meta_key' => 'featured_page',
            'meta_value' => 'homepage',
            'compare' => "!=",
        ),
    ),
);

$posts = get_posts($args);

var_dump($posts);

So the code is exactly the same as before, but this time we are checking if it is a featured post and if the featured_page does NOT equal homepage. This is just an example, but it can be useful once you have a lot of different meta values. In this case we might be pulling featured posts into the homepage if they belong there and then on all the other pages we are pulling in the remaining featured posts that were not included on the home page.

And Finally, Relational Meta Queries Using Relation

The meta query relation parameter is very useful, we can use the relation parameter to determine how we want to check on each meta query, so far we have only used AND, this is default.

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'meta_key' => 'featured_page',
            'meta_value' => 'eventpage',
            'compare' => "=",
        ),
        array(
            'meta_key' => 'featured_page',
            'meta_value' => 'homepage',
            'compare' => "=",
        ),
    ),
);

$posts = get_posts($args);

var_dump($posts);

This example returns any post that has the featured_page field set to homepage OR eventpage. This is useful if we want a page with all of our featured posts on. In this example yes we could just use featured_post = yes, but this is just an example of what we can achieve. Once you start playing around with masses of data and different fields you will start to see just how powerful this is. This can also be incredibly powerful for theme developers if they are looking to pull in different pages to different sections of their themes.

Comments