Wordpress - List your most popular Posts

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!

<?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:

<h2><?php _e('Popular Posts'); ?></h2>
<ul>
	<?php echo(listPopularPosts()); ?>
</ul>

15 comments for Wordpress - List your most popular Posts

Cory Lankin's picture

Awesome post...

Awesome post Smile

Loren's picture

This works well. How would...

This works well. How would you limit the output to the 5 or 10 most commented?

Richard's picture

Hello Loren, You can easily...

Hello Loren,

You can easily change the number listed by changing the value in the SQL Statement. So, below where it says "LIMIT 0 , 5" at the end of the statement, you would change 5 to the number desired. Smile

$result = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");

chaz's picture

It seems as though that if...

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.

Richard's picture

Hello Chaz, You are correct...

Hello Chaz,

You are correct in stating that it queries the whole database. If you wanted to limit so that it queries a specified date range, that’s possible as well. Cheers!

chaz's picture

What if you want to display...

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.

Richard's picture

Hello Chaz, This works on the...

Hello Chaz,

This works on the home/main page as well.

david knight's picture

hi Richard Great code, thanks...

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!

Richard's picture

Hey David, Thanks for your...

Hey David,

Thanks for your comments! You can definitely add more content. You just need to change the SQL Query. For example, the query below will pull the content for the post as well!


$result = $wpdb->get_results("SELECT comment_count, ID, post_title, post_content FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");

Mark's picture

I've been around the web and...

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

Ajay's picture

hi, Just found this and its...

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?

Abhishek Dwivedi's picture

Thanks Richard for the...

Thanks Richard for the interesting tip. I was almost stuck fetching the permalink for my post. Your code helped !!

-Abhi

Dexter's picture

Very nice post and very...

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???

Geost's picture

Hi! Is there any way to get...

Hi! Is there any way to get them sorted by most viewed?

reifenup's picture

This post, I really like,...

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.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.