How To Track & Display Post Views in WordPress Without Any Plugin

In this article, We’ll show you how to count post views and display it on your WordPress post. Moreover, you can track post views in WordPress Admin dashboard without any plugin. We have to quickly create a code snippet that you can use to track post views. Perhaps, you already have seen some blogs that display counter of post views for each post, and you might be wonder how it did!

You have 3 options to track post views. Track only in Front-End side of every single post, track only on back-end Admin dashboard, or both place front-end (blog) and back-end (Admin dashboard). The code will start tracking when you start to create the post. And the page views counter will increase whenever you refreshes or view the post. Let’s start step by step how you can display the total number of views of each post without using any plugin.

Track & Display Post Views on WordPress Posts

Here are four pretty simple steps you have to follow to track page views easier.

Step 1

Add these codes in your themes function.php file. It will count the views when someone refreshes or view the post. Thus, it will implement post views of core functionality.

<?php
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);
    }
}
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";
    }
    if ($count > 1000) {
        return round ( $count / 1000 , 1 ).'K Views';
    } else {
        return $count.' Views';
    }
}
?>

Note: If this is your first time to adding code snippets in WordPress, then we suggest you use Code Snippets Plugin.

Step 2

Same way you need add this codes in your themes function.php file.

<?php
function add_post_views_column($defaults) {
    $defaults['post_views'] = __('Views');
    return $defaults;
}
add_filter('manage_posts_columns', 'add_post_views_column');

function get_post_views($column_name, $id){
    if($column_name === 'post_views') {
        echo getPostViews(get_the_ID());
    }
}
add_action('manage_posts_custom_column', 'get_post_views', 10, 2);
?>

The above code will add column in your WordPress Admin panel. As well as display each post views. However, if you do not want to display in Admin panel then skip this step.

Track post views column in Admin Dashboard
Track post views column in Admin Dashboard

Step 3

Next, you have to add the following code in your single.php file within the WordPress loop (have_posts loop). This code will track the views and set the post views.

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

Notes for W3 Total Cache Fragment Caching in WordPress: If you are using W3 Total Cache plugin then it might be above code would not work. Instead of the above code, use the following code to run setPostViews() will work fine and track each of your page views even after caching enabled.

<!-- mfunc setPostViews(get_the_ID()); --><!-- /mfunc -->

Step 4

At last step, you have to use the following code where you would like to display the number of views in your post loop.

<?php echo getPostViews(get_the_ID()); ?>

Finally, post views counter created. It will work fine, and it’s look following at our end.

Track post views column in Admin Dashboard

We hope you have found this article helpful. Let us know your questions or feedback if any through the comment section in below. You can subscribe our newsletter and get notified when we publish new WordPress articles for free. Moreover, you can explore here other WordPress related articles.

If you like our article, please consider buying a coffee for us.
Thanks for your support!

Support us on Buy me a coffee! Buy me a coffee!



3 Responses Leave a Comment

  1. Great article. Thanks

  2. Thank you for your code,
    code works good, but how can I sort view columns(by view) on WordPress posts or products dashboard?

    1. I use this, only add to the original code. it’s 2 functions and filters:

      function register_sortable_columns( $columns ) {
      $columns[‘post_views’] = ‘post_views’;
      return $columns;
      }
      add_filter( ‘manage_edit-post_sortable_columns’, ‘register_sortable_columns’ );

      function views_columns_orderby( $vars ) {
      if ( isset( $vars[‘orderby’] ) && ‘views’ === $vars[‘orderby’] ) {
      $vars = array_merge( $vars,
      array(
      ‘meta_key’ => ‘post_views’,
      ‘orderby’ => ‘meta_value_num’,
      )
      );
      }
      return $vars;
      }
      add_filter( ‘request’, ‘views_columns_orderby’ );

Join the Discussion.