How To Display Post Views Count On Post Screen In WordPress

As a blogger, we regularly write a post for our blog. Some post gets popular and some are not. But what is a measure to track whether the post is popular or not? Obviously, how many times specific post get viewed by a visitor is a way to know how popular our post is.

In this article, we show you how to track and display post views count on post screen. By this way, one can easily come to know how the each post performing on the Internet.

Display Post Views Count On Post Screen

No Need To Use A Plugin

By following this tutorial, you don’t need to use any plugin for tracking and displaying post count. Use of plugin adds additional work for us. We need to keep eye on plugin all time. You never know when a plugin can break your site if it’s not updated time by time. I always prefer to do own coding wherever possible rather than picking a plugin.

Store The Post View Count In The Database

To display the views count on post screen, we need to first save it in the database. Open your functions.php file and place the below code in it.

function count_post_views() {
	if (is_single()) {
		global $post;
		$post_id = $post->ID;
		$count = 1;
		$post_view_count = get_post_meta($post_id, 'views_count', true);
		if ($post_view_count) {
			$count = $post_view_count + 1;
		}

		update_post_meta($post_id, 'views_count', $count);
	}
}

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

Above code calculate and store view count in the postmeta table attached to a post id. We used the remove_action in the above code to keep correct tracking of count. Without this action, we can’t get correct result for post views as it conflicts with the adjacent post.

Display Post Views Count On Post Screen

At this stage, we have completed the process of storing views count for each post in our database. Now, it’s time to display our views count on the post screen at the dashboard.

First, we need to add a custom column to the post screen. By using the below code in functions.php file, we add our column ‘Post Views’ to the post listing page.

add_filter('manage_post_posts_columns', function ( $columns ) 
{
	if( is_array( $columns ) && ! isset( $columns['post_views'] ) )
	    $columns['post_views'] = __( 'Post Views' );     
	return $columns;
} );

Next, display views count under the column ‘Post Views’ for each post. Place the below code for it.

add_action( 'manage_post_posts_custom_column', function ( $column_name, $post_id ) 
{
    if ( $column_name == 'post_views') {
    	$post_view_count = get_post_meta($post_id, 'views_count', true);
    	$count = $post_view_count ? $post_view_count : 0;
        echo $count;
    }

}, 10, 2 );

So our final code would be as follows.

/*Store Post Views Count*/
function count_post_views() {
	if (is_single()) {
		global $post;
		$post_id = $post->ID;
		$count = 1;
		$post_view_count = get_post_meta($post_id, 'views_count', true);
		if ($post_view_count) {
			$count = $post_view_count + 1;
		}

		update_post_meta($post_id, 'views_count', $count);
	}
}

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

/*Add custom column on post listing table*/
add_filter('manage_post_posts_columns', function ( $columns ) 
{
	if( is_array( $columns ) && ! isset( $columns['post_views'] ) )
	    $columns['post_views'] = __( 'Post Views' );     
	return $columns;
} );

/*Display views count under the custom columns*/
add_action( 'manage_post_posts_custom_column', function ( $column_name, $post_id ) 
{
    if ( $column_name == 'post_views') {
    	$post_view_count = get_post_meta($post_id, 'views_count', true);
    	$count = $post_view_count ? $post_view_count : 0;
        echo $count;
    }

}, 10, 2 );

Add the above code in your functions.php file. By this way, you will be able to track and display post views count on post screen. If you have any questions or suggestions, please leave a comment below. You may also want to read our article on How To Remove Columns From Post Page In WordPress.

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

3 thoughts on “How To Display Post Views Count On Post Screen In WordPress

Leave a Reply

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