ThemeXpert Blog

How To Track & Display WordPress Post Views Without Any Plugin

b2ap3_large_wp-post-track-cover1
Parvez Akther


2 mins

Hopefully, you have already seen some blogs use displaying the total view number for every post to prove their visitors engagement, and now you may want to know how it is done?

Do you want to know how many times a particular post has been viewed and as well as want to show the result to your viewers? In this tutorial, I will show you step by step how you can display the total number of views of a particular post without using any plugin.

This is a three simple steps process and its pretty much easy to follow.

First Step

Add this codes from the following block in your themes function.php file. It will configure your theme to enhance this functionality.


function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

The following code will count the views when someone refreshes or view the post, and it won't discriminate between visitors.

Second Step:

Now add the following line of code in your single.php file within the loop. It will track the views and set the views of each post.

setPostViews(get_the_ID());

Third Step:

Now at the last step use the following line of code where you want to display the view number inside the loop. It will get the post view number from the last step where you call the set function to track the post views.

echo getPostViews(get_the_ID());

The following image manifests that the code works finely on my local server:

Kudos to Wpsnipp for creating such useful snippet.

Conclusion

Hope you have found this article helpful. Let us know your opinion or questions if any through the comment section in below.

20 Top WooCommerce WordPress theme in 2017
50+ Free PSD Website Templates For Corporate, Educ...

Build better website with ThemeXpert

Get access to all premium Joomla templates and
next generation page builder Quix.

Join our club
 

Comments (2)

This comment was minimized by the moderator on the site

Will this only display views from that day that I insert it into the theme or will it show the total number from the time the post was uploaded to the site?

This comment was minimized by the moderator on the site

Very helpful content

There are no comments posted here yet

Leave your comments

  1. Posting comment as a guest. Sign up or login to your account.
Attachments (0 / 3)
Share Your Location