Adding a WordPress Post View Count without a plugin

A short tutorial on how to add a simple post view count to any WordPress blog by yourself. It requires minimum time, and has a great effect on your blog!


Similar tags

server hosting e-commerce google page speed php bug network domain coding tips web development seo explained wordpress css

More posts

Playing with Magento, getting started and some of its features!
03 Feb 2020 8 minutes read 1.2k views

Playing with Magento, getting started and some of its features!

Getting started with Magento can be daunting . That is why many people get easily discouraged, but they shouldn't be. At the end of the day, it is just a system like any other! Here is one part of my experience. Here we will talk about a couple of pros and cons when it comes to this system.

3 things that you need to know before you decide to get into coding as a profession
08 Apr 2020 9 minutes read 2.3k views

3 things that you need to know before you decide to get into coding as a profession

Many people are getting a wrong impression when it comes to this segment as a profession . I need to admit that it really does look quite easy when you are an observer!But have you actually thought about what you really need to become a developer ? Any kind of developer - it doesn't matter if it is a web developer, app developer or any other! Well here is a list of things that you need to know before you get into coding as a career choice!

My first hosting experience and a couple of tricks that can help you a lot!
21 Jan 2020 5 minutes read 1.1k views

My first hosting experience and a couple of tricks that can help you a lot!

Like everybody else, I too have tried different hosting companies. Not because one wasn’t good enough but instead I wanted to experiment a bit and see what is “out there” and try to find the best match possible (at least for my taste).

As you may have seen, many blogs use WordPress and a large percentage of those blogs are showing the so-called "Wordpress post view count" for each blog post. This post is describing modifications that we will implement to your backend.

If you feel comfortable editing code, you can display your post views with a few small changes to your theme files. All you need to do is follow the instructions below:

  • Login to your WordPress Dashboard
  • Go to Appearance > Theme Editor
  • Add this to functions.php file (at the end of the file)
// THIS FUNCTION RETURS NUMBER OF VIEWS FOR A POST
function getPostViewCount(){
    $postID = get_the_ID();
    $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, '1');
          return 1;
    }
    if(intval($count) > 1000){
         return number_format($count/1000,1).'K';
    }elseif(intval($count) > 1000000){
         return number_format($count/1000000,1).'M';
    }else{
         return $count;
    }
}
// THIS FUNCTION COUNTS POST VIEWS
function setPostViewCount() {
    $postID = get_the_ID();
    $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);
    }
}
  • Next, open your single.php page and copy following code inside WHILE LOOP
<?php
//...
while ( have_posts() ) {
  setPostViews();
  //...rest of the code
}
  • Now its time to open your category.php or index.php (or you may even add it to archive.php) depending on your theme
<?php
//....
while ( have_posts() ) {
    echo getPostViewCount().' Views';
//... rest of the code

Here is how it works!

In our functions.php file, we have added two functions. One function will set Post View Count and another one will return it. In the first function, we are using the "get_ID()" function which we can access inside our loop to determine which post is currently being displayed. After we know which post we are dealing with it is time to try and access post_meta. If this is the first time post is seen, we need to set it to 1 - because this is the first time somebody is seeing this post. The next time somebody visits our post, our function will just increment view count for 1.

It is a bit simpler when it comes to displaying that value, we just need to get specific post metadata and display it. If you really want to get into it you can do what I did and show count that is greater than 1.000 like 1k and counter that is greater than 1.000.000 like 1M or even add decimal spaces.

Going a step further

Now, to modify the looks of your "Views counter" you can easily do it by wrapping it into some HTML. Let us say that your theme is using Bootstrap:

?>
<span class="badge badge-warning mb-2">
  <?php echo getPostViewCount(); ?> Views
</span>
<h2 class="mb-4"><?php echo get_the_title(); ?></h2>
<?php

If on the other hand, your theme is not using bootstrap, you can always write your own CSS - most of the themes support adding custom CSS. If not you are left with adding a child theme (which is a bit more complex) or editing your theme style.css (which I wouldn't recommend since your changes will be lost during the next theme update).

And that is how you add your own WordPress post view count by yourself!

As always, #keepCoding

Tired of guessing what's holding your website back?

Our Advanced Tracking Tool provides crystal-clear insights into your Core Web Vitals and its URL's HTTP status. Understand exactly where performance dips and errors occur, empowering you to make data-driven decisions that optimize your site's health, user experience, and ultimately, your bottom line.

START MONITORING FOR FREE

No payment required!