0

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?

2

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."'";
| improve this answer | |
  • Thanks but I tried these codes but both not work. I can't understand why? – Morteza 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
0

I solved with this: https://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;
  }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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