How to Display Popular Posts in WordPress
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:
- Create a new function my_popular_posts($count)
- Add some nice styling to your css file
- 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.
$query = new WP_Query( array(
'orderby' => 'comment_count',
'order' => 'DESC',
'posts_per_page' => $count) );
while($query->have_posts()) : $query->the_post();
echo "<li><a href='".the_permalink()."' title='".the_title()."'>".the_title().
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.
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.