Top

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

August 5, 2010

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:

<ul>
<?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&amp;posts_per_page=10&amp;orderby=comment_count&amp;order=DESC');

while (have_posts()): the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php

endwhile;

wp_reset_query();
?>
</ul>

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

Source: DeluxeBlogTips.com

Tweet This Post

Comments

Got something to say?

Bottom
Article Archive  |  This site is a historical archive of pressography.com, a WordPress plugin and theme blog that published between 2006 and 2012. It is preserved for reference and is not operated by, affiliated with, or endorsed by its original authors. Nothing here is for sale and no form on this site is active. If you are the original owner and would like this domain returned, please get in touch.