WordPress: Show popular posts based on comment count

Do you want to add a tabbed widget to show popular posts based on number of comments on your WordPress website? It is fairly easy to do with a custom WordPress post query. Just add the following code in sidebar.php or in any WordPress theme file where you want the list of popular posts to appear. Change the value in “showposts=5” to your desired number.

<?php 
	$my_query = new WP_Query('orderby=comment_count&showposts=5');
	while ($my_query->have_posts()) : $my_query->the_post();

	if ( has_post_thumbnail() ): ?>
	<a href="<?php the_permalink(); ?>" title="<<?php the_title(); ?>" rel="bookmark">
			<?php // Use the image above post title
			echo get_the_post_thumbnail( $post->ID, 'thumbnail', array('class' => 'thumbnail') ); ?>
	</a>

	<?php endif; ?>

	<h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
	<p><?php the_time('j M Y'); ?></p>
	<p><?php comments_popup_link('0 Comment', '1 Comment', '% Comments'); ?></p>

	<?php endwhile; ?>

	<?php wp_reset_query(); ?>