Show popular posts in WordPress

In the standard setup WordPress is capable of displaying a list with the last 5-10 published posts. However it might also be interesting to show the most popular posts to your users, since these posts might increase the stickiness of your site, and also increases the SEO value of these pages. So the aim of this post is to give you a quick guide to how you can implement a list with the most popular posts to your WordPress blog.

Popularity can be defined by  metrics like  visits, comments, ratings etc. In this example we will base the popularity by the amount of comments. However if you have access to data on views or ratings you just need to adjust the SQL to point to the table where these data can be found.

<h4>Popular Posts</h4>
<ul>
  <?php
  $popular_posts = $wpdb->get_results("SELECT id,post_title FROM {$wpdb->prefix}posts where post_status='publish' ORDER BY comment_count DESC LIMIT 0,5");
  foreach($popular_posts as $post) {
    print "<li><a href='". get_permalink($post->id) ."'>".$post->post_title."</a></li>\n";
  }
  ?>
</ul>

The functionality of this script is pretty straightforward – it finds the 5 posts that are published at the moment and that has the most comments, and then displays the posts in a list with links (permalink) to the posts. You can then paste the link into your sidebar, footer or other place where you find this functionality relevant.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
fold-left fold-right
About the author
Jørgen Nicolaisen has been passionately interested in everything online since 1995. His experience is based on working with small hobby projects as well as high volume websites. Jørgen is currently focused on the PHP based programming framework - Codeigniter, and WordPress naturally

Comments are closed.