1

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.

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; ?>
1
  • anyone here to tell?
    – 10wtaylor
    Commented Mar 29, 2013 at 5:37

1 Answer 1

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');
6
  • 1
    The OP will need to use 'suppress_filters' => FALSE in the get_posts arguments I believe. Maybe you can improve your answer with a fuller example? Commented Mar 29, 2013 at 6:28
  • how to add it ? I have a foreach loop and get_posts array.
    – 10wtaylor
    Commented Mar 29, 2013 at 6:34
  • Before your query just add this function or call this function it work on both ways and it show you post of last 7days.
    – Adi
    Commented Mar 29, 2013 at 6:49
  • Thanks @Adi, it worked. but the only thing is get_posts suppresses filters by default. So, there's need to put suppress_filters => FALSE in get post array.
    – 10wtaylor
    Commented Mar 29, 2013 at 7:30
  • @MereDevelopment can you provide reference url for above answer? I couldn't find it in wordpress codex.
    – 10wtaylor
    Commented Oct 1, 2013 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.