Show Post Views Counter in WordPress Post Page without using Plugin

Show Post Views Count in WordPress Post Page without using Plugin

Probably, You have seen nowadays some bloggers showing the total number of views of their posts on their Blog websites. Because showing the visitors engagement in their article/post justifies the quality of the content they posted. Here we discuss how to show Post Views Counter on WordPress without using Plugin on the post page.

There are many WordPress plugins using which you can integrate the same functionality. But using plugins can slow down the site loading speed. So In this article, we have described how you can easily integrate the same functionally in your blog using coding.

Step 1 : Copy the below code on your theme’s “function.php” file

function getDSPostViews($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 setDSPostViews($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_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

Step 2 : Once done the first step , add the below code on your “single.php” file inside the loop.

setDSPostViews(get_the_ID());

Step 3 : Just paste the below code where you want to show the Views Counter.

echo getDSPostViews(get_the_ID());

For Show the Post Count using Plugin check our another article “How to show post count on WordPress using Plugin?

Hope this article helps you to show Post Views Counter on WordPress on the post view count on your blog page. If any issue arises do comment below. Thank You


Fatal error: Uncaught wfWAFStorageFileException: Unable to save temporary file for atomic writing. in /home/doubtsir/public_html/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php:35 Stack trace: #0 /home/doubtsir/public_html/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php(659): wfWAFStorageFile::atomicFilePutContents('/home/doubtsir/...', '<?php exit('Acc...') #1 [internal function]: wfWAFStorageFile->saveConfig('livewaf') #2 {main} thrown in /home/doubtsir/public_html/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php on line 35