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.

One Comment 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

Leave a Reply

Please log in using one of these methods to post your comment:

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


Get every new post delivered to your Inbox.

Join 255 other followers