Display post views count without WordPress plugin

If you are a blogger and want to display views count on every post in your WordPress blog without plugin, then your are at right place. Mostly bloggers want to display number of hits/views count on their blog post. It’s pretty good to display the popularity of post to visitors and readers. There a lot of plugins available to count post views. But I don’t like them because they are not working properly as desired and also slow down your website speed. There are a lot of article written on this topic. I tried too many codes putting in functions.php, single.php and contents.php etc, but all those code had some issues. Some does not display where I like to be displayed OR caused different type of errors like Fatal Error, Parsing etc. For professional developers and programmers it is easy to handle such type of errors. But being a non-technical it is difficult to fined the proper code and proper location where to put the code in required file. I found a lot of people facing issues while applying those codes in their blogs, because there is no proper description available for non-technical. That’s why I am trying to explain with details so that, everyone even if he is non-technical will also be able to Display post views count without WordPress plugin on blog. Currently I’m using WordPress version 4.4 up-to date and this method works fine. Follow this step by step guide to start counting post views count in WordPress blog.

Key points before adding post views count meta?

  • First download those Php files to your PC via FTP client, in which you are putting code.
  • In case you did something wrong you will have the backup on PC.
  • If get any error during this process simply upload backup files from you PC to website.
  • This is a good and safe way before editing a files a file your blog/website.

Let Start…..

Step 1: Login to you WordPress Dashboard. Open theme editor by going to Apperence > Editor. You will fined all files in editor like Functions.php, Header.php, etc (See snapshot)

1. Apperence Theme Editor

Step 2: Copy following code inside your Functions.php file next to php start tag <?php.

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

See the following snapshot to know the exact location where you have to put the code in Functios.php file.

2. Code Preview functions

Step 3: Copy the following code inside Single.php file before end of Php loop.

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

See this image to know the exact location where to put this code.

3. Set post view Single

Step 4: Post views counter is configured and now just set where you wanna displays post views count. Copy the following code to Content.php file anywhere you wanna displays views count.

<span class=”views”><?php echo getPostViews(get_the_ID()); ?></span>

I wanna display it under post title next to comments.  (See snapshot).

4. Show View

When you configure these three codes properly, it will start displaying views count on every post. I have set views count under post title next to comments. See snapshot4. Show View

It just counts post views on post not pages. Whenever someone visits a post or refresh the post, it will count one view in both cases. This is simple trick to display post views count without WordPress plugin. Please share it on your social networks so that more people get benefit. If have any issue then please comment below. I’ll try my best assist you. Thank you

Spread & Thumbs Up
error1500

4 thoughts on “Display post views count without WordPress plugin

  1. Rockerz

    Usually I do not read post on blogs, but I wish to say that this write-up very compelled me to check
    out and do it! Your writing taste has been amazed me. Thanks,very nice article.

    Reply

Leave a Reply

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

* Copy This Password *

* Type Or Paste Password Here *

773 Spam Comments Blocked so far by Spam Free Wordpress