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'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:

<ul>
<?php
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');

query_posts('post_type=post&posts_per_page=5&orderby=comment_count&order=DESC');

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

endwhile;

wp_reset_query();
?>
</ul>

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.

Thanks!

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.

add comment

1 Answer 1

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
add comment

Your Answer

 
discard

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