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 Comments


  1. Ok. Once 3.7 is out on the street for a month or so, you better show us a plugin or widget that shows off what these advanced queries can do.

    Report


  2. 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

    Report


    1. @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!

      Report


  3. Hmmm… those examples don’t make it seem like a particularly useful addition. But that said, this is WordPress and I’m sure that someone will prove me wrong in no time.

    Report


  4. 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 )

    Report


  5. @chris mccoy – Neat! I’m thinking about using a widget that shows a link to a post we published on this date 365 days ago. Looks like that widget would be easy to configure if it tapped into these date queries.

    Report



  6. 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.

    Report

Comments are closed.