© 2009 aut0poietic

WordPress: Date Range (hackish)

Hold on a minute there, Bucky!!!

This hackish monstrosity is no longer needed. I’ve found a better, non-editing-my-wordpress-source-files way of doing this as suggested by wp_user and created it as a plugin. Go get the that and stop reading this hack!!!

For some stupid reason, I find myself needing to use WordPress as an event organizer; You know, you have a list of events happening on x, y, z dates in the future, and you want to list them as the current items in the “blog.”

Well, I found out recently that WordPress doesn’t like this. It’s understandable: Post dates that are in the future are scheduled to be posted in the future and have a status of “future” not “publish” which makes sense — you wouldn’t want them to be listed on the front page, nor in your sidebar.

However, there is a plug-in for Wordpress that solves this little problem: The Future Is Now. It changes all of your future posts to have a status to published, even if it’s a future post.

But for an event organizer, you typically want to list only the events that have not happened yet. Unfortunately, the method for custom post queries in WordPress, query_posts(), will not allow you to query for a date range. Month, Year, and Day/Date are all you get.  I ran into this very problem on a project, and found a simple, if not slightly hackish, solution.

*****WARNING *****

The solution has you editing your core WordPress files!!! Don’t be stupid: Back up the file, pay careful attention to the source, and if you don’t know what you’re doing or are even mildly freaked out by this, don’t screw with it.

The file wp-includes/query.php is the file that formats all queries in WordPress. So I simply added the code to handle date ranges. Break out your editor, here’s the massively simple code to make this work.

On line 1654 (of my query.php file), within the function get_posts(), just after the if block for handling the m parameter (the month), I added the following code:

...
if ( $q['date_range'] )
{
	$where .= " AND $wpdb->posts.post_date BETWEEN  '" .
		       date("Y-m-d", strtotime($q['start_date']))."'  AND '" .
		       date("Y-m-d", strtotime($q['end_date']))."'";
}

...

Yup, that’s it, in all it’s 3 line glory. This little statement has added 3 new parameters to the query_posts method: date_range, start_date, and end_date. Once you’ve added the above code, you can place the call to query_posts in any of your theme files, such as your index.php, right above “The Loop,” like so:

...

query_posts("date_range=1&start_date=2009-02-02&end_date=2009-08-02&order=ASC");
if (have_posts()) :
while (have_posts()) : the_post(); 

...

The three parameters must be used in combination — you can’t leave out a parameter.

There’s room for improvement: MySQL (on my installation) seems to get bent out of shape when start_date is larger than end_date. Also, combining these with the built-in Wordpress time parameters (hour, minute, day, year, month, etc.) produce undefined behavior — It could explode and be the end of the known universe. Either that or the query just wouldn’t make sense.

Hope this is of use to someone. Took me forever to figure out that WordPress didn’t do this by default.

(on a side note, I really need to fix my code display — completely forgot about it in this theme)

4 Comments

  1. RaiulBaztepo
    Posted April 1, 2009 at 3:16 am | #

    Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I’v just started to learn this language ;)
    See you!
    Your, Raiul Baztepo

  2. PiterKokoniz
    Posted April 8, 2009 at 4:38 pm | #

    Hello ! ;)
    My name is Piter Kokoniz. oOnly want to tell, that I like your blog very much!
    And want to ask you: is this blog your hobby?
    Sorry for my bad english:)
    Tnx!
    Your Piter

  3. MymntentY
    Posted April 8, 2009 at 11:38 pm | #

    hm.. love it

  4. Posted February 23, 2010 at 8:08 am | #

    You could use a ‘posts_where filter’, then you’d not have any need to edit a core file.

One Trackback

  1. By The Shrubbloggers » Shrubbloggers 2.0 on May 25, 2009 at 7:12 pm

    [...] his own, I sprang into action. A new survey of Wordpress possibilities revealed that, in February, someone might have solved the archive problem, finally making it possible to create dueling author feeds for old blog entries. A test of his [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>