WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


share|improve this question

migrated from stackoverflow.com Jul 2 '12 at 13:22

This question came from our site for professional and enthusiast programmers.

up vote 0 down vote accepted

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' );
share|improve this answer

Your Answer


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