How to Display Popular Posts in WordPress

0 Flares 0 Flares ×

You might have seen popular posts widget on many websites running on WordPress. This widgets show the popular posts on your web site based on the comment counts of the posts. So today in this WordPress tutorial we will see how to display most popular posts in WordPress without using any plugins. This is just a simple tutorial and based on this tutorial you can build your own plugin as well.

Remember here that we are going to measure the popularity of the posts by comment count, it is because we are assuming that the more is the number of comments the more popular is the post. So let’s see how to display most popular posts in WordPress based on comment count. 

How to Display Popular Posts in WordPress (Coding) 

So what we are going to do is to create a new function in the functions.php file of your theme and then we will call this function from where we want to display the popular posts in your theme. So be fore moving ahead let me point out what all things we are going to do:

  1. Create a new function my_popular_posts($count)
  2. Add some nice styling to your css file
  3. Call the function from where you want to display the popular posts 

1. Create the function my_popular_posts($count)

Open your the functions.php file of your WordPress theme and add a new function called my_popular_posts($count) just before end of file, you can take any name of your choice but make sure that whatever name you are giving does not exists. Below is the complete code snippet that you can add just before the end of your php file.

function my_popular_posts($count)
{
$query = new WP_Query( array(
'orderby' => 'comment_count',
'order' => 'DESC',
'posts_per_page' => $count) );
if($query->have_posts())
{
echo "<ul>";
while($query->have_posts()) : $query->the_post();
echo "<li><a href='".the_permalink()."' title='".the_title()."'>".the_title().
"</a></li>";
endwhile;
echo "</ul>";
}
else
{
echo "<p>No popular posts found<p>";
}
}

Explanation: In the above function we are creating a WP_Query with order by comment count. Our function accepts an argument $count (this is to specify the number of popular posts). The function retrieves posts based on comment counts and displays in descending order.

2. Add Some Nice Styling to Your CSS file     

If you want you can also add some nice styling to your css file and attach the class to the <li in the above function code in echo part. Else leave the this step and the style will be inherited from your themes css.

3. Call the Function from Where you Want to Display Popular Posts 

Now our function ready and we can call this function from anywhere in your theme where you want to display the popular posts. You can add below code to call function from your theme.

//Call the popular posts function.
my_popular_posts(5);

The above call to function will display 5 popular posts on your blog. This function call you can make from any sidebar widget as well where you have provision to add php codes (you can use text widget).  The above code you can add to anywhere in your theme where you want to display the posts.

I hope this tutorial will help you in building your own plugin to display most popular post. This you can take as a base for your plugin development. You can also read : Mastering Must Know Skills For a WordPress Plugin Developer

Guru

+Ayodhyanath Guru holds a B.Tech degree in Electrical Engineering and has worked with various prestigious clients in the IT industry and presently working as a Software Engineer. He is a part time blogger and presently authors the Jafaloo.Com blog. Being a tech enthusiast Guru likes to surf the web and blogs about interesting technical topics like How-To guides, freewares, Tutorials, Software, Gadgets, web applications etc. Apart from blogging he likes coding in Java/J2EE and PHP.

You may also like...

2 Responses

  1. arnonate says:

    Looks like you are missing a declaration. This code produces a mess for me.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 0 Flares ×