DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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 Comments

  1. 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?

  2. And this not work too:

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

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

  4. 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?

Leave a Comment

Your email address will not be published. Required fields are marked *