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 a specific post gets 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 the post screen. In this way, one can easily come to know how 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. The use of a plugin adds additional work for us. We need to keep an eye on the plugin at all times. You never know when a plugin can break your site if it’s not updated time by time. I always prefer to do my own coding wherever possible rather than picking a plugin.

Store The Post View Count In The Database

To display the views count on the 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);

The above code calculates and stores the view count in the postmeta table attached to a post id. We used the remove_action in the above code to keep the correct tracking of the count. Without this action, we can’t get the 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 the view 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 the 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 the 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 to your functions.php file. In this way, you will be able to track and display post views count on the 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 *