Title: posts_where filter using custom fields for dates
Last modified: August 19, 2016

---

# posts_where filter using custom fields for dates

 *  Resolved [maduroblanco](https://wordpress.org/support/users/maduroblanco/)
 * (@maduroblanco)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/posts_where-filter-using-custom-fields-for-dates/)
 * Currently I have the loop listing posts between two dates using the posts_where
   filter –
 *     ```
       function filter_where( $where = '' ) {
       		$where .= " AND post_date >= '2000-01-01' AND post_date <= 2004-12-31' ";
       		return where;
       	}
       	add_filter('posts_where', 'filter_where');
       	query_posts($query_string);
       	while (have_posts()) :
             	the_post();
             	the_content();
       	endwhile;
       ```
   
 * In a meta_key called ‘original_date’ I have stored a different date for each 
   post that I wish to use instead of the post_date. How do I correctly call up 
   this meta_key into the $where query to fulfil the same purpose as the post_date?
 * `$where .= " AND [meta_key=original_date] >= '2000-01-01' AND [meta_key=original_date]
   <= '2004-12-31' ";`
 * For example, the following SQL query doesn’t seem to work, even though correct
   values (2001-10-29, 2004-11-03, etc.) are present in the ‘original_date’ meta_key
   of the posts –
 *     ```
       global $wpdb;
       	$where .= " AND (SELECT meta_value FROM $wpdb->postmeta WHERE meta_key='original_date' ) >= '2000-01-01' AND (SELECT meta_value FROM $wpdb->postmeta WHERE meta_key='original_date' ) <= '2004-12-31' ";
       ```
   
 * whereas the following works fine on the same posts using their post_date as reference–
 * `$where .= " AND post_date >= '2000-01-01' AND post_date <= 2005-12-31' ";`
 * Perhaps the meta_value array needs to be stripped of some extraneous material
   in order to format the results into the same form as post_date? How to go about
   this?

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

 *  [ucfknight10](https://wordpress.org/support/users/ucfknight10/)
 * (@ucfknight10)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/posts_where-filter-using-custom-fields-for-dates/#post-1818528)
 * can you not just set the publish date of the post to the “original date”, or 
   do you need them to be different?
 *  Thread Starter [maduroblanco](https://wordpress.org/support/users/maduroblanco/)
 * (@maduroblanco)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/posts_where-filter-using-custom-fields-for-dates/#post-1818532)
 * Yes. However, the idea is to show an older, original date of the article under
   the title, but still be able to publish this article to the front of the queue(
   like sticky posts), but also to be able to pre-schedule them for any time in 
   the future. So, the ‘Publish on’ date and actual article date (original_date)
   have to be kept separate.
 * The solution has since been provided here –
    [http://wordpress.stackexchange.com/questions/5546/how-to-correctly-call-custom-field-dates-into-a-posts-where-filter-using-sql-stat](http://wordpress.stackexchange.com/questions/5546/how-to-correctly-call-custom-field-dates-into-a-posts-where-filter-using-sql-stat)
 *  [ucfknight10](https://wordpress.org/support/users/ucfknight10/)
 * (@ucfknight10)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/posts_where-filter-using-custom-fields-for-dates/#post-1818533)
 * very well

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

The topic ‘posts_where filter using custom fields for dates’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 2 participants
 * Last reply from: [ucfknight10](https://wordpress.org/support/users/ucfknight10/)
 * Last activity: [15 years, 6 months ago](https://wordpress.org/support/topic/posts_where-filter-using-custom-fields-for-dates/#post-1818533)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
