I'm working on implementing a WP feature that shows the most commented posts from the last X days (e.g. lat 2 days). I've used the following code I found on the web (and here on SO), which does the job:

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');


while (have_posts()): the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>



The problem that occurs is that the wp_reset_querry apparently doesn't work, as all category listings, tag cloud etc after the above code are messed up -- what happens is that if there's another WP code that lists posts in a category, it'll list only the X days that the above code mentions, despite having wp_reset_querry.

I'm trying to figure out why that is and what I'm doing wrong here? I'd appreciate any help. Also, if there's an easier or better way of displaying the most commented posts over the last X days, please let me know.


1 Answer 1


Try removing the filter after your query, so it does not affect other queries around your page.

//.. your loop...
endwhile; wp_reset_query();
remove_filter( 'posts_where', 'filter_where' );

Your Answer

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