How to Display Page View Count in WordPress (2 Methods)

display page view count

There are a couple of tricks available to enhance the visitor’s engagement in your website. One of the ways is to display page view count in WordPress. It doesn’t only show a specific number but also creates an eagerness to read the post on your page. 

If you don’t yet know how to display page view count in WordPress, learn it from this post. In this post, I am going to show you the whole process. After acquiring the process, apply it to your website and increase engagement. 

You can display page view count in Two different ways. You can display it either via plugins or via functions.php or custom coding. 

Method 1: Display Page View Count Using Plugin

This is the easiest and most recommended way.

Step 1: Install Post View Counter

At first install the Post View Counter plugin from the WordPress plugins directory. Don’t forget to activate it after you download it. 

Step 2: Customize Settings

Now navigate to settings -> Post Views. From here, customize the settings as you need. And if you like your counter in the top position, toggle the display tab and select position as ‘before the content’.

You are done. In this way, you can easily display page view count. 

Method 2: Display Page View Count Using Custom Coding

In this procedure, you have to write some code in your themes function.php file. First, navigate to Appearance -> Theme Editor and select functions.php(Theme Functions)

Now insert the following code before the closing tag and click on Update File.

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);

Now open single.php(Single Post).

Paste the following code in the loop.

setPostViews(get_the_ID());

Now the last step, copy and paste the following code where you want to display the number of page views. 

echo getPostViews(get_the_ID());

Now, your post should look like this.

Conclusion

Hope you have done it correctly to display your page view count in the WordPress site of yours. To minimize problems, it is recommended to use plugins instead of custom codes. If you liked the post, please share it with your friends, and don’t forget to share this post with your friends.

Was this helpful?

Thanks for your feedback!

5 responses to “How to Display Page View Count in WordPress (2 Methods)”

  1. Hoa sen vàng Avatar
    Hoa sen vàng

    Hi, With this plugin configuration can work with WooCommerce, I am looking for a solution for the website with Product View Count

    1. Istiak Rayhan Avatar
      Istiak Rayhan

      No, it’s only for post views. For product view, you can try this plugin: https://wordpress.org/plugins/product-view-count/

  2. Zapatero Avatar
    Zapatero

    Great job! Is it possible to sort the posts by views?

    1. Istiak Rayhan Avatar
      Istiak Rayhan

      It might be possible as the plugin has a ‘Sortable Admin Column’ option.

  3. abolfazl Avatar
    abolfazl

    thank you
    This code works great

Leave a Reply

Your email address will not be published. Required fields are marked *

Introducing New Table Block Plugin - Tableberg

X
Need help?