Post Pic

WordPress loop: Get posts published between two particular dates

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.

4 Responses

Nov 17 2011 01:15

Thanks for trick
I put dates to var but not work:

$s = “2011-10-23″;
$e = “2011-11-17″;

$where .= ” AND post_date >= ‘”.$s.”‘ AND post_date = ‘$s’ AND post_date <= '$e'";

whats wrong?

Nov 17 2011 01:18

And this not work too:

$where .= ” AND post_date >= ‘$s’ AND post_date <= '$e'";

Jun 28 2012 10:41

Use $where .= ” AND post_date BETWEEN ’2009-05-01′ AND ’2009-05-15′”;

Jun 28 2012 18:30

is there any chance to use this in twice wp query? because if i try to used twice with diferent dates the page become blank… any sugestion?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox