Adding Post Views Count in WordPress

There are quite a few plugins out there than can keep the post views count in WordPress. However, you may have known by now that I don’t like using Plugin. Luckily writing a custom function that can track the post view in WP is not that hard.

In this post, I will show you how you can add such functionality on your site without any plugin. So, let’s get started. In order to keep the track of your post views we have to initialize a function every time when someone visits your page/post. This is how that function looks like.

<?php
function track_post_views($postid) {
   $key = 'views_count';
   $value = get_post_meta($postid, $key, true);
   if ($value == '') {
      $value = 0;
      delete_post_meta($postid, $key);
      add_post_meta($postid, $key, '0');
   } else {
      $value++;
      update_post_meta($postid, $key, $value); }
} ?>

You can simply copy and paste this snippet on your themes functions.php file and update the page. At this point we still need to grab the Post ID number of every single posts or page that are being viewed. To grab the post ID, simply place the following snippet on your single.php or page.php file.

<?php
if (function_exists('track_post_views')) {
   track_post_views(get_the_ID()); } 
?>

or simply the following line. Either way it should work. However, using function_exists is always recommended as a fail-safe mechanism.

<?php track_post_views(get_the_ID()); ?>

So, basically we are done with the tracking code. Now, we need to display the post count. We can easily do that using the following snippet. Remember, it will work only for pages(page.php) or posts(single.php) where you have the “track_post_views” function.

<?php echo get_post_meta($post->ID,'views_count', true); ?>

That’s all folks. We are done with the coding part.

Explanation

The overall concept of this function is very simple yet very effective. All I am doing here is grabbing the ID number of the post or page that is being viewed. Using that ID number we are adding post meta data with the key “views_count” on our database through “track_post_views” function. It initially checks if the post meta data (using “views_count” key) is available or not. If it is empty, it adds the meta value as “0”.

However, next time it would add the meta value by 1 if anyone visits the same page or posts. That’s all we are doing with this function. Later on, I am simply displaying the post meta value simply echoing the “get_post_meta” function.

Usage

This function could also be really handy if you want to display list of popular posts (based on views) on your sidebar section of your site. I will surely write on that one as well on my next post. So, stay tuned.

That’s all for today. If you have question or comment regarding this topic. Feel free to drop me a link from the comment section below. Thanks.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.