get_posts不超过X天 - Wordpress

时间:2023-01-15 10:27:46

In my Wordpress site, I use this get_posts code:

在我的Wordpress网站中,我使用此get_posts代码:

get_posts(
        array (
            'numberposts' => 5,
            'orderby'=>'comment_count',
            'order'=>'DESC',
            'post_type'   => array ( 'post' )
        )

How do I filter it so that the posts are not older than 10 days? So it should only list posts from the past 10 days.

如何过滤它以使帖子不超过10天?所以它应该只列出过去10天的帖子。

2 个解决方案

#1


26  

As of 3.7 you can use date_query http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

从3.7开始,您可以使用date_query http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

So it would look like:

所以它看起来像:

$args = array(
    'posts_per_page' => 5,
    'post_type' => 'post',
    'orderby' => 'comment_count',
    'order' => 'DESC',
    'date_query' => array(
        'after' => date('Y-m-d', strtotime('-10 days')) 
    )
); 
$posts = get_posts($args);

#2


2  

The exemple from the doc should work just fine. get_posts() uses WP_Query() behind the scene to make the actual request. For your case the modified example should look something like this:

doc的例子应该可以正常工作。 get_posts()使用场景后面的WP_Query()来发出实际请求。对于您的情况,修改后的示例应如下所示:

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
    // posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-10 days')) . "'";
    return $where;
}

add_filter( 'posts_where', 'filter_where' );
$query = get_posts(array (
            'numberposts' => 5,
            'orderby'=>'comment_count',
            'order'=>'DESC',
            'post_type'   => array ( 'post' )
         ));
remove_filter( 'posts_where', 'filter_where' );

#1


26  

As of 3.7 you can use date_query http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

从3.7开始,您可以使用date_query http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

So it would look like:

所以它看起来像:

$args = array(
    'posts_per_page' => 5,
    'post_type' => 'post',
    'orderby' => 'comment_count',
    'order' => 'DESC',
    'date_query' => array(
        'after' => date('Y-m-d', strtotime('-10 days')) 
    )
); 
$posts = get_posts($args);

#2


2  

The exemple from the doc should work just fine. get_posts() uses WP_Query() behind the scene to make the actual request. For your case the modified example should look something like this:

doc的例子应该可以正常工作。 get_posts()使用场景后面的WP_Query()来发出实际请求。对于您的情况,修改后的示例应如下所示:

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
    // posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-10 days')) . "'";
    return $where;
}

add_filter( 'posts_where', 'filter_where' );
$query = get_posts(array (
            'numberposts' => 5,
            'orderby'=>'comment_count',
            'order'=>'DESC',
            'post_type'   => array ( 'post' )
         ));
remove_filter( 'posts_where', 'filter_where' );