Weekly Most Commented Posts: How To Display A List On Your Blog

StevenOne responseWordPress

There are lots of blogs which are displaying a list of the most popular posts in the past X days. These posts’ toplists are generally based on the number of pageviews, but I believe that getting the most commented posts of the week is a more accurate way of displaying the popular posts.

To display a list of the most commented posts of the week on your WordPress blog, we need to make a custom MySQL query by using WordPress’ $wpdb object.

Paste the following code where you want the list to be displayed:

<?php
$days = 7; //To fetch posts published during the last 7 days

$today = date("Y-m-d H:i:s");
$daysago = date("Y-m-d H:i:s",strtotime(date('Y-m-j H:i:s')) - ($days * 24 * 60 * 60));

$result = $wpdb->get_results("SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN $daysago AND $today ORDER BY comment_count DESC LIMIT 0 , 10");

echo '<ul>';
foreach ($result as $topten) {
  $postid = $topten->ID;
  $title = $topten->post_title;
  $commentcount = $topten->comment_count;
  if ($commentcount != 0) {
  ?>
    <li><a href="<?php echo get_permalink($postid); ?>"><?php echo $title ?></a></li>
  <?php }
}
echo '</ul>';
?>

Tip: You can edit the variable on the line 2 if you want to display a list of the most commented posts in a different timeframe. Simply replace 7 by another number of days.

Learn how I made $33,543 online in the past 12 months.

Simply input your best email below to get started.

One Comment

  1. Buysellwordpress

    Well, I’m sure that this can be a perfect opportunity to every owner of WordPress blog

Learn how I made $33,543 online in the past 12 months.

Simply input your best email below to get started.

No thanks, I have enough money