Using The New Date Queries In WordPress 3.7

Josh Pollock - October 27, 2013

WordPress 3.7 added several cool new features. With automatic updates getting most of the attention, many of the other new features have been lost. One cool new feature is the new date queries added to WP_Query courtesy of a patch by Alex Mills, AKA Viper007Bond. This new parameter for WP_Query allows you to easily query by a post’s published date. Combined with the other parameters in the WP_Query class you can now do easily do some pretty cool things based on a post’s date.

In this tutorial I will introduce you to the new date queries by showing you how to use them to show posts published one year ago, the most commented on posts in the last week and posts scheduled to be posted tomorrow.

Keep in mind that I am leaving how to construct the results for these query up to you. I’d imagine the best place for them would be in a widget. A slider could be nice to, especially to highlight the most commented on posts at the top of the page.

Posts Published On This Day Last Year

Our first query will get all posts published exactly one year ago–a cool way for established blogs to get new life out of older content. To begin we will get the current date and storing it in the array $today using the php function getdate(). By default it will return the current date.
Our array has many useful keys, for this query we will be using ‘year’, which is the current year, “mon”, which is the current month, and “mday” which is the current day. Since these keys are all integers we can simply subtract 1 from $today["year"] to get last year, and use the month and day keys to get the same date.

<?php
$today = getdate();
$args = array(
'date_query' => array(
array(
//simply subtract one year from current year
'year' => $today["year"]-1,
'month' => $today["mon"],
'day' => $today["mday"],
),
),
);
$query = new WP_Query( $args );
?>

Besides that simple subtraction, this query is actually identical to what we would do to query for all posts from the current day. Bonus query!

Most Commented Posts In The Last Week

Our second query combines the date query with three other parameters, used to order the results by the number of comments. It also uses additional parameters for the date query itself to specify a date range using ‘before’ and ‘after’ keys in the array for ‘date_query’. We will also need to set ‘inclusive’ to true so that any posts that match ‘before’ or ‘after’ exactly are returned.
If we were to use the same method in this query as we did in the one before, we would simply use 'day' => $today["mday"]-7, but we would run into problems if today’s date was the 7th or earlier. One of the things that makes this new date_query so cool is that you can use strings like ‘2 weeks ago’, ‘3 days ago’ or ‘today’ to specify dates. Check out how easy that makes setting the date range in this query:

<?php
$args = array(
'date_query' => array(
//set date ranges with strings!
'after' => '1 week ago',
'before' => 'today',
//allow exact matches to be returned
'inclusive' => true,
),
'orderby' => 'comment_count',
'order' => 'DESC',
'posts_per_page' => '5',
'paged' => '1',
);
$query = new WP_Query($args);
?>

Notice that after the ‘date_query’ I set ‘orderby’ to ‘comment_count’ and ‘order’ to descending. This will return posts in order by most commented on, which was the point of all of this. I then set it to only return the first 5 posts. You can adjust ‘posts_per_page’ to suit your needs.

Posts That Will Be Published Tomorrow

Section updated to use current_time(‘timestamp’) instead of time(), which is best practice, as pointed out by Zlatko Zlatev.

Our final query is designed to get scheduled posts, but only the ones scheduled to be be posted tomorrow. If you show just the title and excerpt from the posts, you will have a “teaser” that can help generate return visits to your site.

In our first query we got the current date with getdate(). That function can be used to get a date array for any date passed to it as its first parameter. We will need to get the date for two days from the current date, so we can return posts before that date–ie tomorrow. To do this we will get the current UNIX time in seconds using current_time(‘timestamp’) and add two days to it. WordPress 3.5 introduced some helpful constants for units of time in seconds, one of which was DAY_IN_SECONDS. We can use this constant, times 2, plus the current time, which we will retrieve using the php function time().
From there our date_query looks almost identical to the first one, we are just using a date in the future. We also need to add a parameter for post status to make sure we only get scheduled posts, not drafts or pending posts with dates in the future.

<?php
//get an array of the date 2 days from now
$twodayslater = getdate( current_time(‘timestamp’) + 2*DAY_IN_SECONDS );
$args = array(
'date_query' => array(
'before' => array(
'year' => $twodayslater['year'],
'month' => $twodayslater['mon'],
'day' => $twodayslater['mday'],
),
'inclusive' => false,
),
'post_status' => 'future'
);
$query = new WP_Query( $args );
?>

What Else Can You Do?

Those are three cool little queries to demonstrate the power of the the new date queries in WordPress 3.7. I’d love to see what interesting implementations of these queries you come up with or what cool other ways you find for using date queries. Leave a link to your creations in the comments or tweet them to me @Josh412.

More Information