Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this code for display posts between two particular dates:

$s = '2011-10-23';
$e = '2011-11-17';

function filter_where($where = '') {
        $where .= ' AND post_date >= $s AND post_date <= $e';
    return $where;
  }
add_filter('posts_where', 'filter_where');
query_posts($query_string);

But that's not work until change to this:

function filter_where($where = '') {
        $where .= ' AND post_date >= '2011-10-23' AND post_date <= '2011-11-17'';
    return $where;
  }
add_filter('posts_where', 'filter_where');
query_posts($query_string);

What's wrong in first code?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

I think its because the SQL statement needs the dates inside the quotation marks. Try

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

That should work, otherwise, try

$where .= " AND post_date >= '".$s."' AND post_date <= '".$e."'";
share|improve this answer
    
Thanks but I tried these codes but both not work. I can't understand why? –  Nulled Nov 16 '11 at 23:15
    
mmm odd. maybe try var_dumping the where string and use wp_die to see whats going on? –  Stephen Harris Nov 16 '11 at 23:28
    
OK , Thanks I'll try that. –  Nulled Nov 16 '11 at 23:35

I solved with this: http://stackoverflow.com/a/7334678

$GLOBALS['start_date'] = date('Y-m-d', ($startdate);
$GLOBALS['end_date'] = date('Y-m-d', ($enddate);

function post_between_date($where = '') {
    $where .= " AND post_date >= '" . date('Y-m-d', strtotime($GLOBALS['start_date'])) . "' AND post_date <= '" . date('Y-m-d', strtotime($GLOBALS['end_date'])) . "'";
    return $where;
  }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.