Return wordpress posts for various date ranges
May 14, 2009 · Print This Article
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 return posts for various date ranges.
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 according to your needs.
<?php
function filter_where($where = '') {
//posts in the last 30 days
//$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
//posts 30 to 60 days old
//$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
//posts for May 8 to May 16, 2008
$where .= " AND post_date >= '2008-05-08' AND post_date < '2008-05-16'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>





















Comments
Got something to say?
You must be logged in to post a comment.