Codeable info

Display The Most Popular Posts By Number of Comments

Posted on by in WordPress Tutorials

Displaying a list of your most commented posts can be a great way to drive more traffic to your most valuable resources. This little code snippet, which can be pasted anywhere in your WordPress template files, will pull in a list of the five most commented upon posts on your site and display them in an unordered list.

<ul>
<?php
	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) { >
			<li><a href="<?php echo get_permalink($postid);>" title="<?php echo $title ?>"><!--show the post title as a link-->
			<?php echo $title ?></a> {<?php echo $commentcount ?>}</li><!--show the number of comments on the post-->
		<?php }
	} ?>
</ul>

Looking for an example of what this does?

Popular Post List
Take a look at the tabbed area in the sidebar of this site. See the tab that says Popular?

This code snippet will allow you to generate a list of the most popular posts and display them anyway you choose, just apply your own CSS styling.

There are a lot of plugins out there that do this, but this method allows you to achieve the same result without using a plugin, which results in your site loading just a little faster. This is also an excellent trick for theme developers wishing to incorporate popular post lists as a feature in their themes, without requiring a plugin.

)
Codeable info

Comments (25)

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Comment by Fatih Toprak says:

didint work

Codeable info