Better WordPress Popular Posts

Better WordPress Popular Posts

Better WordPress Popular Posts by Commets Count Without SQL Query

A list of the popular posts of the blog is a standard feature in many wordpress blogs. There are quite a few wordpress plugins that offer this feature and it’s very simple to install a plugin to display your most popular posts.

There a many wordpress hacks out there that advise you how to do it without using a plugin.

The system is the  to direct query wordpress mysql database and sort posts by comment count in order to achieve this. Since WordPress 2.9 there is no need for more direct queries. WordPress 2.9 added  ‘orderby=comment_count‘ argument to query_posts() which allow us to sort retrieved posts by comment count. So if you want to avoid yet another direct sql query in order to create a list for your most popular post this is how you can do it:

First  create the function. I named mine  dd_most_popular. Copy the code below and paste it to your functions.php file.

<?php function dd_most_popular() {

global $post;

 $most_popular = get_posts('numberposts=10&order=DESC&orderby=comment_count'); ?>

 	<ul>

 	<?php foreach($most_popular as $post) :

   	setup_postdata($post);?>

    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

  	<?php endforeach; ?>

 	</ul>

<?php }?>

Now, wherever you want to display the popular posts list, place the following code. You can do it in the sidebar or other wordpress theme files.

<?php dd_most_popular(); ?>

Stay updated I may write a plugin!

Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • Technorati
  • Twitter
Posted in: blog

2 Comments on “ Better WordPress Popular Posts ”

  1. Discount sunglasses May 30, 2010 at 12:47 pm

    Nice article. Thank you for this info

  2. Christian Louboutin Shoes May 30, 2010 at 5:10 pm

    Great article Thank

    you so much!

Leave a Reply

You must be logged in to post a comment.