If you want to keep real-time track of your WordPress post views / hit by user then you can use this code. Below code snippet/plugin will gives stats for each post.
Step1: Put this into functions.php file.
function getCrunchifyPostViews($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 setCrunchifyPostViews($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.
Place this snippet below “setPostViews” within the single.php inside the loop.
<?php
setCrunchifyPostViews(get_the_ID());
?>
Another must read:
- How to Display Most Recently Updated Post/Page in WordPress
- WordPress: Prevent Your WordPress Post Image Being Too Large
Step 3.
Place this snippet below within the template where you would like to display the number of views.
<?php
echo getCrunchifyPostViews(get_the_ID());
?>

