WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Variable in WP-Query (6 posts)

  1. caledoniaman
    Member
    Posted 1 year ago #

    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.

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this:

    $where .= " AND post_date >= '1970-01-01' AND post_date <= '".$today."'";
  3. caledoniaman
    Member
    Posted 1 year ago #

    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.

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    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');
  5. caledoniaman
    Member
    Posted 1 year ago #

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

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you got it resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic