This function below creates a list of links of the top commented posts on your WordPress blog. It builds this list based on the posts that have the most comments. Place it in your footer or sidebar item!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php function listPopularPosts() { global $wpdb; $strBuidler = ''; $result = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5"); foreach ($result as $post) { setup_postdata($post); $postId = $post->ID; $title = $post->post_title; $commentCount = $post->comment_count; if ($commentCount != 0) { $strBuidler .= '<li>'; $strBuidler .= '<a href="' . get_permalink($postId) . '" title="' . $title . '">' . $title . '</a> '; $strBuidler .= '(' . $commentCount . ')'; $strBuidler .= '</li>'; } } return $strBuidler; } ?> |
Call the function in your sidebar of footer like this:
1 2 3 4 |
<h2><?php _e('Popular Posts'); ?></h2> <ul> <?php echo(listPopularPosts()); ?> </ul> |
Awesome post 🙂
This works well. How would you limit the output to the 5 or 10 most commented?
It seems as though that if you had a lot of posts that your script would have to query the whole DB to find just the top 5 most actively discussed.
1. Wouldn’t it be better to change the query to limit to the last 60 days so that not only would you only have the more relevant articles but also so you don’t have to query the whole DB? Or would you still have to query the db anyway?
2. Ideally, it would be nice to set this up to rebuild as a cron job once per day and just include what is built by the daily job… no? Do wordpress offer anything like that? For instance, why build the category list every single time the page is hit when mine hasn’t changed in the last 6 months.
What if you want to display them on the main page in the loop instead of the sidebar? It seems as though this only works in the disbar.
hi Richard
Great code, thanks for that. One question if that’s ok. How would you add more details to the list? Id love to be able to add more of the content, perhaps even categories. Is this possible? Thanks again for the code!
I’ve been around the web and around the web some more and it seems everyone is doing a great job at building this function.
However something that is missing is a tutorial on how to get it to display like the normal post loop that everyone has on their homepage or category pages.
This only shows a list of the titles with links.
Can you do a tutorial on how to display like that?
I’m betting it will be immensely popular if you do.
Speak soon hopefully.
Mark
hi,
Just found this and its exactly what I’m after.
Is there any way of assigning a different class to each so that i can use a different font size for each?
Thanks Richard for the interesting tip. I was almost stuck fetching the permalink for my post. Your code helped !!
-Abhi
Very nice post and very helpful too.
I’m new to wordpress theme development and I just want to know how would I include the post thumbnails???
Hi! Is there any way to get them sorted by most viewed?
This post, I really like, hope you write this more…keep your good write…and wish we can make a good relationship, would you like to exchange link with me??? I’m very grateful if you will…thanks.