0

我正在尝试使用 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 但到目前为止我一直无法理解我做错了什么。任何帮助都会很棒:)谢谢

4

1 回答 1

0

这应该可以,不需要过滤器。
更多时间参数

$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
于 2012-12-05T12:24:14.723 回答