Title: Loop posts between from current post date and a set date?
Last modified: August 19, 2016

---

# Loop posts between from current post date and a set date?

 *  Resolved [patrik92gmailcom](https://wordpress.org/support/users/patrik92gmailcom/)
 * (@patrik92gmailcom)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/loop-posts-between-from-current-post-date-and-a-set-date/)
 * I’m trying to make a loop that displays 5 posts between two dates. This shouldn’t
   be hard but one of the dates is the date of the current post and is therefore
   a variable…
 * This is what I’ve come up with but it doesn’t work. No errors but just shows 
   all posts.
 *     ```
       function filter_where($where = '') {
               $where .= ' AND post_date >= "2000-01-01" AND post_date <= "' . $date .'"';
           return $where;
         }
       add_filter('posts_where', 'filter_where');
       query_posts('showposts=5');
       ```
   
 * The $date is retrieved from another loop like this:
    `$date = the_date('Y-m-d');`
 * and I’ve tried to define $date manually but it won’t work:
    `$date = "2010-01-
   01";`
 * Would be grateful for any ideas!

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/loop-posts-between-from-current-post-date-and-a-set-date/#post-1347997)
 * Try `global $date;` inside the function (after the `function filter_where($where
   = '') {` statement).
 *  Thread Starter [patrik92gmailcom](https://wordpress.org/support/users/patrik92gmailcom/)
 * (@patrik92gmailcom)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/loop-posts-between-from-current-post-date-and-a-set-date/#post-1348049)
 * Thank you! This help alot,but only if i defined $date manually…
 * This works: $date = “2010-01-01”;
    But this doesn’t: $date = the_date(‘Y-m-d’);
 * I don’t see why there should be a difference?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/loop-posts-between-from-current-post-date-and-a-set-date/#post-1348198)
 * use `$date = date('Y-m-d');`
 * [http://php.net/manual/en/function.date.php](http://php.net/manual/en/function.date.php)
 *  Thread Starter [patrik92gmailcom](https://wordpress.org/support/users/patrik92gmailcom/)
 * (@patrik92gmailcom)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/loop-posts-between-from-current-post-date-and-a-set-date/#post-1348215)
 * I solved it, `date()` won’t work because I didn’t want the current date.
    `$date
   = $post->post_date_gmt;` fixed the problem

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Loop posts between from current post date and a set date?’ is closed 
to new replies.

## Tags

 * [date](https://wordpress.org/support/topic-tag/date/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [variables](https://wordpress.org/support/topic-tag/variables/)

 * 4 replies
 * 2 participants
 * Last reply from: [patrik92gmailcom](https://wordpress.org/support/users/patrik92gmailcom/)
 * Last activity: [16 years, 4 months ago](https://wordpress.org/support/topic/loop-posts-between-from-current-post-date-and-a-set-date/#post-1348215)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
