How to Display The Most Popular Posts in WordPress

Displays the most popular posts in the sidebar are very helpful for visitors to find out the most viewed post of your site. You can get the most popular posts using simple WordPress most viewed posts query.

There are main plugins that can do this but you don’t need a plugin for simple things like this in fact. You can use these code snippet and then you can style it exactly as you want. This tutorial will show you the simple way to display the most popular posts and comments by views without using any plugin.

WordPress Code To Add Popular Posts (by views)

The below snippet will use the post Meta for storing the post views count. Using the stored Meta value, we’ll fetch the posts according to the views count. You need to modify only two files functions.php and single.php. After that, you can display the most popular posts using simple posts query.

  1. Open the functions.php file of the activated theme and add the following code.

setPostViews() function add or update the post meta with post_views_count meta key.

2. Open the single.php file from activated theme directory and place the setPostViews() function inside the loop.

3. Display the Most Viewed Posts

The following query will fetch the posts based on the post_views_count meta key value. Place the following code in the sidebar or where you want to display the most popular posts list.

That’s all, hopefully this article helped you know how to display the most popular post in WordPress without using plugin. If you have any further questions for us, don’t hesitate to put it up in the comments. See you again in orther tutorials!

Leave a Reply

Your email address will not be published. Required fields are marked *