TecnoNerds

TecnoNerds

Be A Technical Nerd

Count Post View Without Plugin in WordPress With W3 Total Cache

 Count Post View Without Plugin

Introduction

There is one thing I want to implement in my website is Post View Counter. And I am using WordPress for my CMS, and that doesn’t come with WordPress, it’s something you have to make on your own. So every time someone visits any post on our website, then we want to increment the number of views by one. Then we need to store that amount of views in a database. And then query and bring back that views within our post. So how do we Count Post View Without Plugin?


Count Post View Without Plugin


Count Post View Without Plugin – Step 1 –

First thing first, copy the following code which I give you below and paste it in your functions.php file.
//PHP - FUNCTIONS.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);
}
}
// Remove issues with prefetching adding extra views
remove_action
( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);


Count Post View Without Plugin – Step 2 –

This part of the code will set the post view of your post. So paste it in single.php inside the WordPress loop.

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


Count Post View Without Plugin – Step 3 –

This part of this code will display your views on your post. So place it where you want to show your post views ( single.php/index.php ). Place this code within the loop

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

[Source]

So, that is all for count post View without plugin. But here is one problem you will face –

The view counter will work fine, but if you are using a caching plugin like W3 Total Cache, this view counter will not work. The functionsetPostViews() would never run. Here is a little fix for that. Open up your W3 Total Cache and do whatever you want but don’t enable page cache in General Settings. Or you can download my W3 Total Cache settings from this link. You can apply my settings on your website.

General Settings –

Count Post View Without Plugin in WordPress With W3 Total Cache

 

I think this article will help you. Hope you now successfully create the post counter and count post view without plugin. I am using the same process to calculate post view on my website, and it works fine, you can see that. But if you still have some problem, if your counter even is not increasing then feel free to comment. I will find another solution for you.

Share This Post ~

Leave a Reply

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

*

*
*