Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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; ?>
share|improve this question
    
anyone here to tell? –  10wtaylor Mar 29 '13 at 5:37
add comment

1 Answer

up vote 1 down vote accepted

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');
share|improve this answer
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? –  Mere Development Mar 29 '13 at 6:28
    
how to add it ? I have a foreach loop and get_posts array. –  10wtaylor Mar 29 '13 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 Mar 29 '13 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 Mar 29 '13 at 7:30
    
@MereDevelopment can you provide reference url for above answer? I couldn't find it in wordpress codex. –  10wtaylor Oct 1 '13 at 11:06
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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