getButterfly Logo getButterfly code wrangling since 2005

As the end of the year is getting closer and closer, you might want to show a top ten most commented articles. By modifying the code you could display a top 3, top 15, top 100, or whatever number you like.

Here’s how:

<h2>Most commented articles in 2009</h2>
$result = $wpdb->get_results(“SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN ‘2009-01-01’ AND ‘2009-12-31’ ORDER BY comment_count DESC LIMIT 0 , 10″);
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 }}?>

Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.

If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

2 thoughts on “WordPress Most Commented Articles During The Year

Leave a reply

Love programming?

Learn about the most amazing things. Get smarter everyday!