
WordPress loop is very powerful, as well as the query_posts() function, which allow you to specify some parameters for the loop to retrieve posts. Though, there’s no built-in function or parameter to get posts between two dates. Let’s solve that.

WordPress loop is very powerful, as well as the query_posts() function, which allow you to specify some parameters for the loop to retrieve posts. Though, there’s no built-in function or parameter to get posts between two dates. Let’s solve that.
Open your index.php file and find the loop. Just before the loop starts, paste the following code. Of course, don't forget to change the dates on line 3 according to your needs.
<?php
function filter_where($where = '') {
$where .= " AND post_date >= '2009-05-01' AND post_date <= '2009-05-15'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>
Credit: WordPress Codex.
29 Responses
nice tips. I’ll try. btw, thanks for sharing
Thanks a lot. Very useful
Another great tip. Your site will achieve randomness which is good for SEO. THanks, again.
Interesting recipe. You can do so much with Wordpress!
Very usefull, thanks
Interesting. Could you use this to display the next post in the future?
I know you can use post_status=future but I needed to use “The Future is Now” plugin so that doesn’t work anymore.
I gotta tell you, I found this blog about a month ago, and have been bookmarking literally every entry! Great, simple stuff- Keep it rocking!
Hehe, this one is seriously very interesting. You never know for which reason you may come into situation to filter posts by custom dates, and in such a case, this is perfect solution.
It didn’t work for me. I wanted to mail you but then I realised that I forgot to past a part of the last line. Rewrote it and everything’s working perfectly. Good job.
Thanks, its work…
Trackbacks: