Views Count for WordPress Post Page Without Plugin

In this article, we going to do views count for WordPress post page without a plugin. Based on the postID here, we going to count the post views every time the user visits the post. This function will work under the post visits based, then using IF condition we going to use operator count++ to increase the post views count. By doing without plugin that helps us to maintain our website performance in good health.
Use the below code in your functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function getPostViews($postID){ $counterData = 'post_views_count'; $count = get_post_meta($postID, $counterData, true); if($count==''){ delete_post_meta($postID, $counterData); add_post_meta($postID, $counterData, '0'); return "0 View"; } return $count.' Views'; } function setPostViews($postID) { $counterData = 'post_views_count'; $count = get_post_meta($postID, $counterData, true); if($count==''){ $count = 0; delete_post_meta($postID, $counterData); add_post_meta($postID, $counterData, '0'); }else{ $count++; update_post_meta($postID, $counterData, $count); } } remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); |
Once you add the above code now set the post views in single.php post loop.
1 |
<?php setPostViews(get_the_ID()); ?> |
The final step to display the no of Post views, for that use the below code in single.php template wherever in the loop you like to display the post views count.
1 |
<?php echo getPostViews(get_the_ID()); ?> |
Conclusion:
I hope this article will help you to do the views count for your WordPress post page. If you have any doubts let me know in the comment section and if you love this article share it with your friends.
Post CountPost ViewsWithout Plugin
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]