Skip to content

How To Add A Post View Counter In WordPress

Learn how to add a post view counter in WordPress without JavaScript or any third-party tracking code library.

How To Add A Post View Counter In WordPress

You may often noticed a counter showing number of views for a blog post in some websites. This tiny nice feature doesn’t come pre-packed with WordPress, however, there are several List of Post View Counter Plugins to add post view counter to your site. If you don’t want to rely on third-party plugins, or you want to integrate post view counter in a custom theme, you can code your own. It’s just a few lines of PHP code, without JavaScript or any tracking code library slowing down the page load speed. This guide will show you how to add a post view counter to your WordPress site using plugin or custom coding.

Post View Counter Using a plugin

First you need to install and activate the Post Views Counter plugin. Simply go to Plugins > Add New and search for Post View Counter.

Once activated, you need to go to the Settings > Post Views Counter option to setup the plugin. You’ll need to choose which post types you want to be tracked (default is posts only), and counting mode. If you are using a caching plugin (Like W3 Total Cache or WP Super Cache) choose JavaScript or REST API counting mode as caching will interfere with updating counter in PHP mode.

Post Views Counter Plugin's General Settings
Post Views Counter Plugin’s General Settings

Display settings will allow you modify how and where the counter will appear. You can set a label to display before the counter and include a icon as well. You can also choose between automatic placement (before or after post content) and manual placement (using a shortcode). Once done, save changes and head over to any of the post/pages (where you chose to display the counter) to view the counter.

Post Views Counter Plugin's Display Settings
Post Views Counter Plugin’s Display Settings

Post View Counter Using Custom Code

If you prefer to use your own code to track post views, you can do so by storing the views in post meta and increment it by one per page load. Below is a pretty basic code that display a post view counter. You can modify it to suite your need.

function pc_post_view_counter( $post_ID ) {
    if ( is_singular() ) { //We want the counter for single posts/pages/CPTs only
	
		//Get the views count from post meta
		$count = get_post_meta($post_ID, '_post_views_count', true);
		
		//If the the Post Custom Field value is empty, set it 0 initially
		if( !isset( $count) || empty( $count ) ){
			$count = 0;
			//Set the counter meta value to 0
			update_post_meta( $post_ID, '_post_views_count', '0' );
			//Return View Count
			return sprintf( __( '<span>%1s</span> Views', 'pixellcode' ), $count );
		} else {
			//Increment the counter by 1 on each visit.
			$count++;
			
			//Update the views count meta value
			update_post_meta( $post_ID, 'post_views_count' , $count);
			
			//Return View Count
			return sprintf( _n( '<span>%2s</span> View', '<span>%2s</span> Views', $count, 'pixellcode' ), $count );
		}
	
	}
}

Now just call the function anywhere within the loop and pass the post ID as the parameter.

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

The only problem with this code is when some browser pre-fetches adjacent posts links added by WordPress in page head. That automatically increments the view counts for those pages without even loading it. To get rid of that problem, the adjacent posts links should be removed.

remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

Custom Post View Counter

Optionally you can add a column displaying post view counter in admin edit pages. This bit of code does that.

/* Register The Column */
function pc_post_column_views($columns) {
    $columns['post_views'] =__( 'Views', 'pixellcode');
    return $columns;
}
add_filter('manage_edit-post_columns', 'pc_post_column_views' );

/* Retrieve The View Count and Display It*/
function pc_post_custom_column_views($column, $post_id ){
    if($column === 'post_views'){
		echo get_post_meta( $post_id, '_post_views_count', true);
    }
}
add_action('manage_posts_custom_column', 'pc_post_custom_column_views', 10, 2);

The code above creats a column in All Posts/Pages/CPT page in admin. Additionally we can make the column sortable in case we need to sort the posts by view count.

/* Make the column sortable */
function pc_sortable_views( $sortable_columns ){
    $sortable_columns['post_views'] = 'post_view';
    return $sortable_columns;
}
add_filter('manage_edit-post_sortable_columns', 'pc_sortable_views' );

/* Change Order By On Demand */
function pc_views_orderby( $query ) {
    if( ! is_admin() )
        return;
    $orderby = $query-&amp;gt;get( 'orderby');
    if( 'post_view' == $orderby ) {
        $query->set('meta_key','_post_views_count');
 $query->set('orderby','meta_value_num');
    }
}
add_action( 'pre_get_posts', 'pc_views_orderby' );

That’s how you can add a simple post view counter on your blog. If you face any problem adding the counter in your blog, do let us know in comments and we will try to help you as much as possible.

About Abhik()

Website: www.pixellcode.com

I am a WordPress fanatic and a full time blogger. I love to talk about WordPress and Online Marketing. I also work as a freelancer and have a bunch of awesome people as my client.

Your Comment

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

Copyright © 2016 - 2018 PixellCode. All Rights Reserved