Display Your Most Popular Posts In the Last 30 Days Without a Plugin

Display Your Most Popular Posts In the Last 30 Days Without a Plugin

Here’s a bit of code that I’ve found to be handy.

Sometimes, you have a site that is already running so many plugins and you hesitate to add another one on top just to display popular posts. Instead, you can use this to change your WordPress loop to show your most popular posts by comment count in the last 30 days:

<?php
function filter_where($where = '') {
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC');
while (have_posts()): the_post(); ?>
<a title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?>
<?php
endwhile;
wp_reset_query();
?>

Just add this to your template where you want to use it. No plugins necessary!

Source: DeluxeBlogTips.com

Tags: