【问题标题】:WP_Query() and custom filterWP_Query() 和自定义过滤器
【发布时间】:2012-12-05 11:22:58
【问题描述】:

我正在尝试使用 WP_Query() 在我的 wordpress 博客上显示一些帖子。 这个想法是在最近 30 天内发布的帖子中显示 9 个随机选择的帖子。 由于某些原因,这似乎不起作用。

这是我使用的代码:

    function filter_where_custom( $where = '' ) {
        // posts in the last 30 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
        return $where;
    }

    $args = "orderby=rand&posts_per_page=9";
    $my_query = new WP_Query();
    add_filter('posts_where', 'filter_where_custom');
    $my_query->query($args);

我认为问题出在使用 add_filter 但到目前为止我一直无法理解我做错了什么。 任何帮助都会很棒:) 谢谢

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    这应该可以工作,不需要过滤器。
    更多关于time params

    $args = array(
      'year' => date('Y', strtotime('-30 days')),
      'monthnum' => date('m', strtotime('-30 days')),
      'day' => date('d', strtotime('-30 days')),
      'orderby' => 'rand',
      'posts_per_page' => 9
    );
    $my_query = new WP_Query($args);
    
    // start your custom $my_query->have_posts() loop
    

    【讨论】:

      猜你喜欢
      • 2020-04-20
      • 2016-04-17
      • 2023-04-04
      • 1970-01-01
      • 2021-02-13
      • 2013-05-09
      • 1970-01-01
      • 2011-01-24
      • 2013-04-11
      相关资源
      最近更新 更多