Custom Display Most Popular Posts by Views in WordPress

138

Displays the most viewed posts at the sidebar are very helpful for visitors to find out the most popular post of your site. This tutorial will show you the simple way to display the most popular posts by views without using any plugin. You can get the most popular posts using simple WordPress most viewed posts query.

We’ll 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.

functions.php File

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.

single.php File

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

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.

 

Leave A Reply

Your email address will not be published.