Post Views using Post Meta in WordPress

This Post discusses how you can track Post Views in WordPress without using Plugin by using Post Meta. Post Views can be shown in Home Page and along with the Post itself but I will show Post Views in Posts page on Admin Screen. In the end, I’ll make this column sortable.

Open functions.php of your theme and add below functions.

function get_post_views($post_id){
    $count_key = 'post_views_count';
    $count = get_post_meta($post_id, $count_key, true);
    if($count==''){
        delete_post_meta($post_id, $count_key);
        add_post_meta($post_id, $count_key, '0');
        return "0 View";
    }
    return $count . ' Views';
}

function set_post_views($post_id) {
    $count_key = 'post_views_count';
    $count = get_post_meta($post_id, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($post_id, $count_key);
        add_post_meta($post_id, $count_key, '0');
    }else{
        $count++;
        update_post_meta($post_id, $count_key, $count);
    }
}

// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

Now In order to increment post counter, include following code in the loop of single.php in your theme.

set_post_views(get_the_ID());

If you are using W3 Total Cache, then use below code instead of above code. This will enable you to track the post views even when caching is enabled.

<!-- mfunc set_post_views(get_the_ID()); --><!-- /mfunc -->

Now if you want to show Post Views in your Public Site, then use below code where ever you like.

echo get_post_views(get_the_ID());

In order to show Post Views as a Column in Posts Page in Admin Screen, add below filter and action in functions.php of your theme.

// register custom column
add_filter( 'manage_posts_columns', 'custom_posts_table_head' );

function custom_posts_table_head( $columns ) {

    $columns['views']  = 'Post Views';
    
    return $columns;

}

// values to display in custom column
add_action( 'manage_posts_custom_column', 'custom_posts_table_content', 10, 2 );

function custom_posts_table_content( $column_name, $post_id ) {

    if( $column_name == 'views' ) {
        $count_key = 'post_views_count';
	    $count = get_post_meta($post_id, $count_key, true);
	    if($count==''){
	        delete_post_meta($post_id, $count_key);
	        add_post_meta($post_id, $count_key, '0');
	        echo "0";
	    }	    
        echo $count;
    }    
}

Now your Admin screen will include a column with Post Views as shown in below Figure

WordPress Custom Column
WordPress Custom Column

To make Post Views column sortable, add below filters in functions.php of your theme.

add_filter( 'manage_edit-post_sortable_columns', 'custom_posts_table_sort_columns' );
function custom_posts_table_sort_columns( $columns ) {
  $columns['views'] = 'views';
  return $columns;
}

add_filter( 'request', 'custom_post_view_column_orderby' );
function custom_post_view_column_orderby( $vars ) {
    if ( isset( $vars['orderby'] ) && 'views' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'post_views_count',
            'orderby' => 'meta_value_num'
        ) );
    }

    return $vars;
}
Comments
  1. Posted by test1

Leave a Reply to test1 Cancel reply

Your email address will not be published.