Hack: Show List of Posts With Most Comments

A common thing to need is a list of your most active posts, those being the ones with the most comments. You can get that with a simple call to get_posts using the right arguments. Here is a shortcode that returns the list for you to use in a post, or perhaps in a widget if you have shortcodes enabled for widgets.

add_shortcode('gettopcommentposts','wpshortcode_get_top_comment_posts');
function wpshortcode_get_top_comment_posts() {
	$out = '';
	$posts = get_posts(array('numberposts'=>10, 'orderby'=>'comment_count'));
	foreach ($posts as $post) {
		$link = get_permalink($post->ID);
		$title = get_the_title($post->ID);
		$num = get_comments_number($post->ID);
		$out .= "<li><a href=\"$link\">$title ($num)</a></li>";
	}
	return $out;
}

This function can be placed in your functions.php file or your custom plugin. You can easily modify the number of posts returned and the HTML format of each line.

Leave a Reply

Your email address will not be published. Required fields are marked *