Wordpress Technology

Wordpress Tips, Tutorials, Hacks

List of The Most Commented Posts

As you know the number of installed plugins affects the loading blog time.
To display most commented posts without any plugin insert next code into template files (index.php, sidebar.php and so on.)

get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $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 >"></a> ()</li>


5 in line 2 is number of links displayed!

Another Way

Next code looks more elegant.

<?php $popular = new WP_Query('orderby=comment_count&showposts=7'); ?>
<?php if ($popular->have_posts()) : ?>
		<?php while ($popular->have_posts()) : $popular->the_post(); ?>
	  <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
		<?php endwhile; ?>
<?php endif; ?>

7 means number of posts to display.


Both comments and pings are currently closed.

© 2009 | Made by Vancouver Web Design | Internet Marketing