WP_Query: Get Posts by Relative Date

Someone asked me today how to use WP_Query to get posts by a relative date. The codex provides an example on how to achieve this. Heres what is shown on the codex.

I feel that its sloppy and not very flexible. Having to add the filter and remove it each time you want to do this is ugly. I solved this some time ago and had to dig through some old code to find it.

Ultimately what I have done is the same solution, using the same filter, and utilizing strtotime() to get the relative date. However  i’ve wrapped it into a simple “since” parameter that can be used on any query. Just a little nicer and simpler to use.

This technique can easily be adapted to other custom parameters which result in modifications to the where or join filters.

About these ads

3 Comments on “WP_Query: Get Posts by Relative Date”

  1. I always find it annoying to filter for querying, but that’s a nice trick filtering on a query_var. Also see #18694

  2. These are genuinely great ideas in concerning blogging.
    You have touched some nice things here. Any way keep up wrinting.

  3. Marko says:

    I see a lot of interesting articles on your website.
    You have to spend a lot of time writing, i know how to save you
    a lot of work, there is a tool that creates readable, SEO friendly posts in couple of seconds,
    just search in google – k2 unlimited content


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 253 other followers