Limit popular posts by days

I am using this get_posts array to retrieve top 15 popular posts by comments. The problem is that it shows those all those highly commented posts from the day posts were added into blog. So, the list sometimes changes but most of the time remains same.

How can I limit/show posts of the last 7 days?
This would show most commented posts of the last week.

Read More

Thanks

Here is my code:

global $post;

$args = array(
'orderby' => 'comment_count',
'order' => 'DESC' ,
'numberposts' => 15);

$popular_posts = get_posts( $args );

foreach( $popular_posts as $post ) :

if (has_post_thumbnail()) { ?>
<li>

<div class="widgetimg">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('widgetimg-thumb'); ?></a>
</div>

</li>
<?php } endforeach; ?>

Related posts

Leave a Reply

1 comment

  1. Here is a function which show the posts of last 7days or you can change it according to your requirement.

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