• Resolved caledoniaman

    (@caledoniaman)


    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

    (@keesiemeijer)

    Try it with this:

    $where .= " AND post_date >= '1970-01-01' AND post_date <= '".$today."'";

    Thread Starter caledoniaman

    (@caledoniaman)

    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

    (@keesiemeijer)

    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

    (@caledoniaman)

    Fixed. Your help is very much appreciated. Thank you.

    Moderator keesiemeijer

    (@keesiemeijer)

    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.