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.

10

10 responses to “WordPress 3.7 Introduces Advanced Date Queries”

  1. this feature is great.
    last week @ work i had to display posts from a specific date (past events) on a specific page and future posts (scheduled) in another. the problem was that the scheduled posts couldn`t be read by subscribers….
    now it is much more easier. thx

    • @roscabgdn, i’m trying to do something quite similar on a site. if you have any pointers on how to accomplish this that would be great. i’m especially confused on how to create links to the pages with future and past posts.

      any help is really appreciated!

  2. Yes! I’m sooo looking forward to this feature!

    One of the sites I work on is a newspaper, and it was necessary to display content in specific date dependent ways. For example, the “Breaking News” would only appear a day of it being published (after that it gets moved to general news). As breaking news is rare-ish, it’s quite noticeable when the top of the page features a breaking news story.

    Now, although I could simply write a query to show today’s posts from the “Breaking News” category, that is is only good for that situation. What if I want the last two days? Or what if I want all stories from last week?

    It’s always smarter to write “dumb” code. Code that is generic, that can take any parameters, not hard coded to a specific one.

    So, I figured how hard could it be? Freaking hard! Even with WP’s instructions on injecting clauses into the WHERE statement, it caused no end of trouble. And in fact, is still not working perfectly yet.

    The new date range will be a god send for me and this task. (I haven’t coded it yet simply because of priorities – I mean, my old code is mostly working. :P )

  3. I officially declare my love for Viper007Bond … the true M+007 ;) I can’t wait for a decent widget either since I know I won’t be working on any sites with such requirements for now. A plugin that filters dates on the fly for users would be the icing on the cake, although I am sure there is probably one now.

Newsletter

Subscribe Via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.