Adding Post Views Counter Function in WordPress

0
784
Post-Views-counter-wordpress
Adding Post Views Counter Function on WordPress.

Have you ever thought to add a post views counter to your WordPress site manually? Yeah, I know there are bunch of plugins available that can do so but what if you want to add such feature manually. Well here the solution goes…

By adding a simple function to your WordPress site you can easily display how many times a post has been viewed. You can add this next to your Post Titles or Footer, or anywhere else you want to display. This is a simple function script that will add a post views counter to your posts. You can display it within the post loop or can display outside of the loop. This tutorial has three simple steps, and will take maximum 10 minutes.

Add Post Views Counter Function on WordPress

In the first step you have to add this simple post counter function to your WordPress theme. For this, just open your functions.php file and add this code just after opening PHP tag.

function bt_PostViews( $id, $action ) { // id= post id and action= post count or display views
$btCountMeta = 'bt_post_views'; // Your Custom field that stores the views
$btCount = get_post_meta($id, $btCountMeta, true);
if ( $btCount == '' ) {
if ( $action == 'count' ) {
$btCount = 0; // Count starting from zero
}
delete_post_meta( $id, $btCountMeta );
add_post_meta( $id, $btCountMeta, 0 );
if ( $action == 'display' ) {
echo "0 Views"; // Display post Views (when 0 views), You can add some style here
}
} else {
if ( $action == 'count' ) {
$btCount++;
update_post_meta( $id, $btCountMeta, $btCount );
} else {
echo $btCount . ' Views'; // Display Actual Post Views
}
}
}

Save your functions.php file and now move on STEP 2. 

Adding Views Counter for Single Posts

For Starting post views counter for single posts just open your single.php file and add this small code to the very first line.

<?php echo bt_PostViews( get_the_ID(), 'count' ); ?>

This will enable post counter function for single posts by post id. Now it’s time to display the post counts, for this we have to move on STEP3.

Displaying Post Views Counter

To display post views counter with every post it’s better to add this code just below or aside to post title. Choose the perfect location yourself and add this code.

<?php echo bt_PostViews( $post->ID, 'display' ); ?>

The above php code will display post counts within loop, for some reason if you want to display particular post views outside the loop use this code.

<?php echo bt_PostViews( get_the_ID(), 'display' ); ?>

You can replace get_the_ID() with your particular post id.

How Does Post Views Counter Function Works?

This simple post views counter function for WordPress, works on both inside the loop and outside the loop as well. You can employ it as per your need. On above post views function we are making use of Post Meta function of WordPress. For those who don’t know what the Post Meta is? Post Meta is a built-in function of WordPress for displaying custom fields for the current post.

On above function we are adding a custom post meta $btCountMeta, with post id and meta value ‘0‘. The $action==’display’  displays the ‘o views‘ on the condition of null post views when the post gets a view the function add the new meta value to the post meta and If else function fires to print else statement $action == ‘count’ that displays the actual post views within the loop. For every new view the function update_post_meta updates the meta value for the respected post ID.

 This is so simple script and can be used in your WordPress themes, plugins, sidebar widgets etc..

LEAVE A REPLY

Please enter your comment!
Please enter your name here