WP_Query Tutorial: Retrieve Any Post Using With WP_Query

WP_Query is your buddy, it allows you to get content from the database according to your requirements. You can retrieve all posts from only one category that contain certain tags as well. You could retrieve all pages created and posts which do not have a featured image.

In this article I’ll provide you in-depth look into how WP_Query works – let’s get started!

A Custom Loop

The key to working well with customized queries is mastering the arguments you can pass to them. Before we look at this, let’s create a simple “skeleton” we can use to list posts.

The loop on archive page would look like this:

This works just excellent because before the page is loaded, WordPress has already retrieved the right posts. When we create a customized query we’ll need a customized loop. The code is quite similar, here goes:

We make a new WordPress query using the WP_Query class, passing it parameters to specify the types of posts we need. We then call the have_posts() and the_post() methods on our $result object.

Simple Arguments

Some arguments are easy to understand, such as the tag or tag_id parameter. The former takes a tag slug, the later a tag id. You can also separate multiple ids with commas or use negative ids to indicate that you want to retrieve posts that do not have that particular tag attached.

The above snippet would retrieve posts which have any of the three tags (67,52,32) attached to them, but not the 4th tag(-23). While this technique is highly effective enough for easy needs, I usually use the far more versatile taxonomy query.

The author, author_name, cat, category_name, s (for search terms), post_status, post_type are illustrations of some simpler fields. Some fields, like post_status, need you to pass an array of statuses if you want to use multiple values.

Taxonomy Queries

For easy things, using the described arguments to retrieve content depending on categories or tags is enough but what if you have a custom taxonomy, or you need to combine multiple parameters? The answer depends on the tax_query argument which is an array.

Example:

Here above code snippet retrieve some content from our ‘book’ post type, including some taxonomy parameters. Using two arrays defined within the tax_query. In the query, we specified that to pull books written by author’s 73, 65 or 197 which are not in the sci-fi or thriller category. The relation parameter describes the relationship between the two arrays. In our case, it is an ‘and’ relationships which meaning that both circumstances must be true.

If we used ‘or’ we would have retrieved books which are either not in the sci-fi or thriller category or they are written by the specified three authors.

Meta Queries

Meta queries are very similar to taxonomy queries in structure but they use data from the post meta table to filter posts.

Example:

WordPress stores the ID of the featured image for a post using the _thumbnail_id field. So in above code snippet we retrieve all posts where _thumbnail_id meta value is not empty.

In the above code snippet, we can learn that for compare property and type, we can use a multitude of values which should be familiar from SQL such as ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' (from WordPress 3.5 and up), and 'NOT EXISTS' (from WordPress 3.5 and up) To compare numbers or dates we use type property. And possible values of type property are: ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’. Just similar with taxonomy queries we can stack multiple specifications and then use relation parameter to specify the relationship between them.

Date Parameters

Dates can get a bit complicated, but are very flexible. The WP_Query Documentation has a lot of illustrations if you need more details.

We can use the year, monthnum, w (week of the year), day and a couple of other parameters for retrieving posts from a certain time.

Example 1: Retrieving all posts from the March of 2013

Example 2: Retrieving posts written between 9AM and 5PM on weekdays.

Ordering Results

For ordering results WordPress provide us order_by and order parameters. And order parameter is straightforward, you can use ASC or DESC to order ascending or descending.

The possible values for order_by parameter cab be: ‘none’, ‘ID’, ‘author’, ‘title’, ‘name’, ‘type’, ‘date’, ‘modified’, ‘parent’, ‘rand’, ‘comment_count’, ‘menu_order’, ‘meta_value’, ‘meta_value_num’, ‘post__in’.

Ordering by meta_value can be especially useful. In this case, It is required to specify the meta_key field as well.

Example 1:

Example 2:

Leave a Reply

Your email address will not be published. Required fields are marked *