Post Pic

How to: Display the most commented posts of 2008

Happy new year! To get started with this new year, what about displaying a list of the 10 most commented posts of 2008 to your readers? That’s obviously a great way to give a second life to your old posts.

To display a list of your 10 most commented posts from 2008, simply paste the following code on your blog sidebar, or anywhere on your theme:

<h2>Most commented posts from 2008</h2>
<ul>
<?php
$result = $wpdb->get_results("SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN '2008-01-01' AND '2008-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 }
}
?>
</ul>

That's all! Hope you (and your readers!) like it :)
Best wishes to all of you and your families for 2009, have a happy and sucessfull year!

45 Responses

Jan 02 2009 08:45

Should we use any plugin for this code? Coz I have a related plugin, but get an error with the code they give. So, I plan to add your code here, but what plugin should I use? Thanks

Jan 02 2009 18:18

This is a great tip for the end of the year, but for me it made sense to include the number of comments each post in the list got.

This is an easy tweak, just change this line:
<a href="">

To this:
<a href=""> ()

Jan 03 2009 08:49

So is this what you are using on your Top Commentators sidebar? Seems that way..

Jan 03 2009 08:50

Or at least your not showing 2008, and are now showing 2009 top commentators only. That’s cool, you should state that :)

Jan 03 2009 08:57

Oops, my code got thrashed, that sucks.

All I changed was to echo the comment_count after the title is echoed…

Jan 03 2009 12:43

Hmmm… never thought of this :)

I will give it a try on my personal blog.

Jan 03 2009 19:35

It does not work in my blog !
Dommage.

Jan 04 2009 01:31

=) Happy New Year!

Feb 14 2009 11:54

IT WORKS!! thank you so much. For those who wants the plugin instead, go visit this site

http://mtdewvirus.com/code/wordpress-plugins/

Jun 07 2009 09:59

Obviously I am doing something wrong. I put it in my sidebar, in a text/html box and (as you can expect) the code just shows up as text. So where ‘in my sidebar’ should I put it?

Jun 07 2009 13:08

@ed: Paste the code in sidebar.php, not in a widget ;)

Jun 07 2009 13:14

I already suspected something like that. Thanks a bundle!!

Jul 24 2009 00:57

Hi! I am trying to get a custom field from the postmeta table so that I can display an image next to the link title. Can you help me?

Oct 15 2009 04:30

This is awesome. I’m using it now in my new template. Looks great. Hopefully I’ll get some comments to the older posts.

Dec 28 2009 19:31

I’ve tried using this on a site in a page using Exec PHP but I get the following error:

Fatal error: Call to a member function get_results() on a non-object in /homepages/36/d222495733/htdocs/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()’d code on line 7

Can anyone suggest what might be wrong here?

Jan 01 2010 09:55

How would I ensure that not only the most commented, but also the most recent feature in the results of the query. Adding and “ORDER BY post_date” is not working for me

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required