Title: Variable in WP-Query
Last modified: August 20, 2016

---

# Variable in WP-Query

 *  Resolved [caledoniaman](https://wordpress.org/support/users/caledoniaman/)
 * (@caledoniaman)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/variable-in-wp-query/)
 * Hi guys,
 * I’m trying to pull back a date range of posts. One of the dates in the range 
   is the current date which I am determining using the PHP date function and assigning
   to a variable called $today.
 * If I run this with the date’s hardcoded into the script, it returns the posts
   I want. If however I try and incorporate the variable it doesn’t work. I’ve tried
   surrounding the variable in double quotes as I’ve seen suggested elsewhere but
   it falls over.
 * Anyone point me in the direction of the correct syntax for the code below:-
 *     ```
       function filter_where($where = '') {
         $where .= " AND post_date >= '1970-01-01' AND post_date <= '$today'";
         return $where;
       }
       ```
   
 * Thanks in advance.

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

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/variable-in-wp-query/#post-2829964)
 * Try it with this:
 *     ```
       $where .= " AND post_date >= '1970-01-01' AND post_date <= '".$today."'";
       ```
   
 *  Thread Starter [caledoniaman](https://wordpress.org/support/users/caledoniaman/)
 * (@caledoniaman)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/variable-in-wp-query/#post-2829966)
 * That doesn’t throw me a syntax error but it doesn’t bring back any posts either,
   despite their being posts in the range.
 * I’m echoing the value of $today so I know that it’s showing today’s date.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/variable-in-wp-query/#post-2829968)
 * Try it with the “$today” variable inside the function:
 *     ```
       function filter_where($where = '') {
         $today = date('Y-m-d', strtotime('now'));
         $where .= " AND post_date >= '1970-01-01' AND post_date <= '".$today."'";
         return $where;
       }
       add_filter('posts_where', 'filter_where');
       ```
   
 *  Thread Starter [caledoniaman](https://wordpress.org/support/users/caledoniaman/)
 * (@caledoniaman)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/variable-in-wp-query/#post-2829970)
 * Fixed. Your help is very much appreciated. Thank you.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/variable-in-wp-query/#post-2829971)
 * You’re welcome. I’m glad you got it resolved.

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

The topic ‘Variable in WP-Query’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [13 years, 11 months ago](https://wordpress.org/support/topic/variable-in-wp-query/#post-2829971)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
