This post is about how to Create a most viewed widget for wordpress blog?

This can be done in two steps.

  1. Create a meta field with each post and store a view count whenever a user see your single bog post.
  2. Create a widget which get top posts order by your meta field.

 STEP 1: Create a meta field

Add below code in single.php inside of the loop.

What does it do?

This creates a meta field key ‘views’ and stores its value with each post. When you put this code on single.php whenever a user opens a post it will update ‘view’ by adding 1.

If there is no views at the time it will make it 1 for first time. you can echo $views and see the value increasing with each refresh over single post.

 STEP 2: Create a widget for most viewed post.

Add below code to the functions.php

Now you can see a widget ‘Most Viewed Posts’ in widget listing. Add it to desired place.