If you want to track the individual post views for your blog to find out which posts are the most popular ones and also do not want to hit the performance of the blog by adding another plugin. You could do this with very easy code changes to your theme.
Also Read: Add Related Posts without Plugin
Open your theme’s ‘functions.php’ thesis theme users can add this code to ‘custom_functions.php’
/***********************************************************************
* Functions to track Post Views
**********************************************************************/
function getPostViews($postID){
$count_key = 'post_views';
$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 = 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);
}
}
/**********************************************************************
* Track the Views for the post using wp_head hook
**********************************************************************/
function trackPostViews ($post_id) {
if ( !is_single() ) return;
if ( empty ( $post_id) ) {
global $post;
$post_id = $post->ID;
}
setPostViews($post_id);
}
add_action( 'wp_head', 'trackPostViews');
/*********************************************************************
Show Views in the WP-Admin area
*********************************************************************/
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);
function posts_column_views($defaults){
$defaults['post_views'] = __('Views');
return $defaults;
}
function posts_custom_column_views($column_name, $id){
if($column_name === 'post_views'){
echo getPostViews(get_the_ID());
}
}
Code Explanation
So what is this code doing. Let’s start with getPostViews and setPostViews functions are used to track the views for a post using custom field “post_views” for the post. Function trackPostViews uses the wp_head hook to track the view when ever a post page is viewed. For this to work you theme should support wp_head hook. The last two functions will add a column on the wp-admin Posts screen as shown in the screen shot below.
I am using the same tip to track Post Views on this blog.
Thanks for reading this article and keep your comments coming. ![]()

