Query Improvements in WordPress 4.1
WordPress 4.1 "Dinah" was released in December, and much has been written about the improvements to the user interface: the most talked about change has been the improved writing experience.
But if you're a developer you'll be wanting to know less about that and more about what's changed under the hood. Here I'll demonstrate one of the most interesting changes for developers: improvements to certain types of queries.
The main changes are as follows:
- Support for nested queries has been added for queries on post metadata, dates, and taxonomy terms.
- Extra parameters have been added for querying comments.
- And some bugs have been fixed too!
Let's have a look at the changes.
In previous versions of WordPress, you could use an
OR statement to define queries for taxonomy terms, dates, and metadata. So for example, the following arguments will be used in a query on a recipe site which outputs quick breakfast recipes:
<?php $args = array( 'post_type' => 'post', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'speed', 'field' => 'slug', 'terms' => array( 'quick' ), ), array( 'taxonomy' => 'meal', 'field' => 'slug', 'terms' => array( 'breakfast' ), ), ), ); $query = new WP_Query( $args ); ?>
This looks for the
'meal' taxonomies and outputs posts with the
'breakfast' terms respectively.
But what if you wanted to write a more complex query? Let's say you wanted quick recipes for breakfast and slow recipes for lunch (maybe for someone who wants to get breakfast done quickly so they have more time to cook lunch!). You don't want to use a simple
AND statement to join all the elements of your query, since then you would get slow recipes for breakfast and lunch, for example. And you don't want to use an
OR statement linking all the terms, as you'll get all manner of recipes which only have one of the queried terms along with other ones you want to filter out.
The good news is that now you can do this. To query quick recipes for breakfast and slow recipes for lunch, you'd use the following:
<?php $query = new WP_Query( array( 'tax_query' => array( 'relation' => 'OR', array( 'relation' => 'AND', array( 'taxonomy' => 'meal', 'field' => 'slug', 'terms' => array( 'breakfast' ), ), array( 'taxonomy' => 'speed', 'field' => 'slug', 'terms' => array( 'quick' ), ), ), array( 'relation' => 'AND', array( 'taxonomy' => 'meal', 'field' => 'slug', 'terms' => array( 'lunch' ), ), array( 'taxonomy' => 'speed', 'field' => 'slug', 'terms' => array( 'slow' ), ), ), ), ) ); $query = new WP_Query( $args ); ?>
Here I've used two nested arrays:
- The outer array uses
OR, because we're looking for posts which are either quick breakfast recipes or slow lunch recipes.
- The first nested array looks for posts which are quick breakfast recipes, using
ANDbecause you want the post to have both terms.
- The second nested array looks for slow lunch recipes, again using
Of course you could vary your queries to include multiple taxonomy terms and values, and get as complex as you need.
Applying Nested Queries: Taxonomy Terms, Metadata, and Dates
The example I've given above uses taxonomy terms, but this feature has also been added to date and metadata queries. Metadata is potentially where things could get interesting, as you have the scope for so many values.
The syntax works in exactly the same way for date and metadata queries. For meta queries you replace
meta_query and use
'value' as the parameters. For date queries you replace
date_query and use the date parameters provided in the WordPress Codex.
To query comments, you use the
WP_Comment_Query class in place of the more commonly used
WP_Query class. This class has had eight new parameters added to it:
'author__in': identify comment author (or an array of authors)
'author__not_in': identify comments not by a certain author (or array of authors)
'post_author__in': identify author (or array of authors) of the post the comment was made on
'post_author__not_in': exclude comments made on posts written by particular author or array of authors
'comment__in': comments with a certain ID or array of IDs
'comment__not_in': exclude comments with a certain ID or array of IDs
'post__in': comments made on a post or array of posts (using the post ID)
'post__not_in': exclude comments made on a post or array of posts (using the post ID)
The values used for these are the author ID, comment ID or post ID as appropriate.
Note that the
WP_Comment_Query class now supports nested queries as well.
There have also been a couple of bug fixes which you might find helpful:
- A bug that caused queries to fail when a
date_querywas used along with a
meta_queryhas been fixed.
‘orderby' => 'meta_value’was used when passing a
WP_Query, this used to break the query. This has been fixed.
If you want the lowdown on all the details, you can find it on the make WordPress core site.
These improvements to queries take WordPress a step further to full CMS capability. The ability to use nested queries means that you can output content in much more flexible and complex ways. It will be interesting to see how people use them!
Source: Tuts Plus