Skip to content
How to Count Post Views in WordPress

How to Count Post Views in WordPress

You are a webmaster and you want to keep track of your posts or you want to know if your website has anyone interacting with it? The simplest way is to integrate the article with the view checking feature, so you will easily know the number of views on each blog post on your WordPress website. Using this feature, readers can determine the quality of the article thanks to the number of views and they will feel more secure and want to click to the blog post to read when the number of views is higher than the others. So, in this article, I will guide you how to count post views in WordPress with or without using plugins. Now, let's take a look.

How to count post views in WordPress without using plugins

1. First and foremost, go to the Functions.php file of the currently used theme of WordPress. Then copy the code below and paste it before the closing tag?> of the Functions.php file.

function gt_get_post_view() {
    $count = get_post_meta( get_the_ID(), 'post_views_count', true );
    return "$count views";
}
function gt_set_post_view() {
    $key = 'post_views_count';
    $post_id = get_the_ID();
    $count = (int) get_post_meta( $post_id, $key, true );
    $count++;
    update_post_meta( $post_id, $key, $count );
}
function gt_posts_column_views( $columns ) {
    $columns['post_views'] = 'Views';
    return $columns;
}
function gt_posts_custom_column_views( $column ) {
    if ( $column === 'post_views') {
        echo gt_get_post_view();
    }
}
add_filter( 'manage_posts_columns', 'gt_posts_column_views' );
add_action( 'manage_posts_custom_column', 'gt_posts_custom_column_views' );

2. Then, copy the code below and paste it into single.php file in the while loop.

<?php gt_set_post_view(); ?>

3. Next, copy the following code and paste it where you want to show the number of views

<?= gt_get_post_view(); ?>

4. When it's all done, you will see the post view like this:

Count Post Views in website WordPress

How to count post views in WordPress with the help of a plugin

The Plugin I want to introduce to you is Post Views Counter developed by dFactory. This plugin is one of the most used plugins that helps count post views in WordPress. Currently, it has more than 90,000 active installations. That’s an amazing number. You don’t need to pay anything to get this plugin because it is completely FREE and users can download it from their homepage.

It allows you to display how many times a post, a page or any custom post type had been viewed in a simple, fast and reliable way. It is compatible with WordPress version 4.0 or higher. The plugin is easy to install and use giving users the best experience with very little effort from you as an admin.

Features include:

  • Free and open source
  • 2 methods of collecting post views data: PHP and Javascript
  • Option to select post types for which post views will be counted
  • Option to select page types for which page views will be counted
  • Enable to display post view count column for each of the selected post types
  • Option to set time between counts
  • Display views for each user type (logged in users, guests, selected user roles)
  • Exclude views for each type of visitors (robots, logged in users, guests, selected user roles)
  • Multiple post views position

To get this Post Views Counter, please check it here.

How to count post views in WordPress using Post Views Counter plugin

Step 1:

First of all, as usual, you need to install and activate the Post Views Counter plugin. Click on the button Download on the left of the screen and wait for this plugin to download. After that click to activate it.

Count Post Views in website WordPress

Dowload plugin Count Post Views

Step 2:

Next, move to Settings => Post Views Counter. Here, you will see the setup interface consisting of 2 tabs: General and Display. Start with, the General tab, you can set up it as below.

Count Post Views in website WordPress

The General tab

The General tab includes:

  • Post Types Count: select the type of page you want to display the view counter (pages or posts)
  • Counter Mode: counting method, by PHP or JavaScript
  • Post Views Column: add a column that displays the number of views into the posts admin interface, page
  • Restrict Edit: limits the right to set a view counter (only Admin has permission)
  • Time Between Counts: enter the interval between 2 times to a user's visit. You should leave it to 0
  • Reset Data Interval: Time to store counter data before being reset. You should leave it to 0
  • Flush Object Cache Interval: length of time to delete object cache. You should leave it to 0
  • Exclude IPs: don't count visits from certain IPs
  • WP-PostViews: import counter data from the WP-PostViews plugin
  • Deactivation: enable to delete all plugin data when disabling the plugin

After that click the Save Changes button to save.

Step 3:

Switch to the Display tab. You can set up it as below.

Count Post Views in website WordPress

The Display tab

The Display tab has the following options:

  • Post Views Label: label for the counter.
  • Post Type: the type of article you want to display the counter.
  • Page Type: the type of page you want to display the counter.
  • User Type: the type of user you want to count the number of article views.
  • Position: the position of the counter display. You can choose at the beginning, at the end of the article or manually insert using shortcode [post-views].
  • Display Style: display style. You can choose to display both the icon and the counter's label or choose according to your preferences.
  • Icon Class: select the icon code for the counter. This counter uses the default Dashicon font of WordPress.

Click the Save Changes button to save it after you finished.

Step 4:

When you've done, this is what you will get:

Count Post Views in website WordPress

Conclusion

To sum up, these are two ways to add post views counters in WordPress: using a plugin and without using a plugin. For anyone who doesn’t want to use code to display the number of views of blog posts or pages in WordPress, they can easily use Post View Counter plugin. So, with just a few click setup, you can easily see the views on the posts or pages you want. We hope that this article helps you with your mission to display post views on your site. Thank you for reading.

Leave a Comment






Scroll To Top