【问题标题】:Wordpress: Add filter to query_posts to only show posts with "date" set to today or futureWordpress:向 query_posts 添加过滤器以仅显示“日期”设置为今天或未来的帖子
【发布时间】:2013-12-18 06:18:30
【问题描述】:

我正在使用此查询来显示我的帖子:

$today = getdate();
$year=$today["year"];
$month=$today["mon"];
$day=$today["mday"];

query_posts( $query_string.'order=ASC' . '&post.status=future,publish'.'&year='.$year.'&monthnum='.$month );
?>

现在我想添加一个过滤器,只显示今天发布或将来发布的帖子。

示例:今天是 3 月 22 日。 PostA 于 3 月 1 日发布,PostB 今天发布,PostC 将在 3 月 24 日发布(状态“未来”已在我的查询中,因此仍将显示)。 我的新查询过滤器应该只显示 PostB 和 PostC。

提前非常感谢!

:-)

【问题讨论】:

    标签: wordpress date filter publish


    【解决方案1】:

    试试这个:

    // Create a new filtering function that will add our where clause to the query
      function filter_where( $where = '' ) {
    $today = date('Y-m-d');
      // posts for today and future
    $where .= " AND post_date >= $today ";
    return $where;
      }
    
      add_filter( 'posts_where', 'filter_where' );
      $query = new WP_Query( $query_string );
      remove_filter( 'posts_where', 'filter_where' );
    

    【讨论】:

    • 这会进入functions.php吗?
    • mh 不,它不起作用。但也许我做错了。我只是将它粘贴到我的functions.php中
    【解决方案2】:

    试试这个:

    $archive_year  = get_the_time('Y'); $archive_month = get_the_time('m'); $archive_day   = get_the_time('d'); query_posts('year=' .$archive_year .'&monthnum=' .$archive_month .'&day=' .$archive_day);
    

    【讨论】:

    • 请解释一下这段代码是如何解决问题的。纯代码回答不多。
    猜你喜欢
    • 2014-11-27
    • 1970-01-01
    • 2016-05-13
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 2018-05-07
    相关资源
    最近更新 更多