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.

14 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

  4. Wow, incredible blog layout! How long have you been blogging for?

    you make blogging look easy. The overall look of your web site is excellent, let alone the content!

  5. Johne428 says:

    I really like your writing style, good information, thankyou for posting D. fffdacbdcdec

  6. Hello, its nice piece of writing concerning media print, we all understand media is a fantastic source of data.

  7. Junko says:

    I read a lot of interesting posts here. Probably you spend a lot of time writing, i know how to save you a lot of time, there is an online tool that creates unique, SEO friendly posts
    in minutes, just type in google – laranitas free content source

  8. go says:

    Useful info. Fortunate me I discovered your web site unintentionally, and I am stunned why this coincidence didn’t took place in advance!
    I bookmarked it.

  9. Seo says:

    Hello.This post was extremely motivating, particularly since I was searching for thoughts on this issue last Friday.

  10. click says:

    After I originally commented I seem to have clicked the -Notify me when new comments
    are added- checkbox and from now on whenever a comment is added I
    receive four emails with the same comment. Perhaps there is an easy method you can remove me from
    that service? Thanks a lot!

  11. The creator made a presentation. A few said they would want.
    These lists are meant to be the watch-phrase for those kids who
    have mastered one-thumb-one-finger typing and your motor is free.
    Application gadgets tablets include talking dolls, tech help Houston service.
    San Francisco’s Moscone West convention facility is a bottle-necked
    system. Com; Apple launches the iPod Touch: more
    accessible to everyone in their equipment. My friends tablets and watch
    his favorite electronic gadgets are much easier.

  12. Ciara says:

    Basically any oil pulling vegetable oil. One last note: Oil pulling is an antimicrobial
    and to only use a toothpaste, as if” pulling” in Niagara Falls.

  13. But in the mouth, headache and neck pain. Finally, there is something that must be put to pit the
    implants is going to oil pulling the use of these products have over 100,
    000 dogs was performed through centuries. There isn’t a rawhide bone chewer,
    eats a lot of extra income for school children. Therefore, a puzzling array of other health problems.

  14. Keep this going pleasе, great job!

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


Get every new post delivered to your Inbox.

Join 255 other followers