WordPress 3.7 Introduces Advanced Date Queries

WordPress 3.7 should be arriving any day now. Everyone is excited about WordPress’ new ability to update itself for security and minor releases (and even themes and plugins, if you’re adventurous). The new password strength meter is highlighted on the About page for the release with a live demo that users can play with after they upgrade. But there’s one cool new feature that hasn’t received as much fanfare: advanced date queries.

photo credit: Andrew Morrell Photography via photopin cc
photo credit: Andrew Morrell Photography via photopin cc

You might be wondering what’s so exciting about advanced date queries. Alex Mills, better known online as “Viper007Bond” was the originator of the patch to improve date arguments for WP_Query, a class for querying the database for specific content. Up until now, WordPress had no easy way to get a set of posts that fall within a specific date range.

A few months ago, Alex introduced a patch that adds the ability to get complex date-based queries when fetching both posts and comments from the WordPress database. Code examples can be found on his post where he roughly outlines a few ways one might implement date-based queries.

There are virtually unlimited combinations. Here are a few examples of what is possible with the new advanced date queries:

  • Query all posts written on a specific day over the years, ie. the author’s birhtday
  • Query all the posts written before or after a certain date
  • Query all posts written on Monday mornings
  • Query 5 most recent posts made after midnight but before 5am

You can check out all the date parameters in the codex and browse a few of the examples. Imagine all the fun that could be had here! The advanced date queries should enable developers to do some interesting things with displaying timelines, custom archives, selectively random post widgets and anything else imaginable with date ranges.

Advanced date queries replaces bulkier methods used in the past, such as adding a filter to posts_where. How did we ever live without it? Check out the canonical discussion on Trac to explore the evolution of this feature and put it to good use in WordPress 3.7.

There are 10 comments

Comments are closed.