This snippet is one of the most useful I’ve ever used.

It allows you to fully customize the shown columns in the posts screen, whether it’s a regular ‘post’ type or a custom one.

Adding a new column

Step 1/2 – add the column to the column list & set its label

add_filter('manage_post_posts_columns', function ( $columns ) 
{
    if( is_array( $columns ) && ! isset( $columns['visits'] ) )
        $columns['visits'] = 'Total Visits';     
    return $columns;
} );

Line 1: manage_post_posts_columns = here you need to replace ‘post’ with the post type you want to add this column to, could be the regular ‘post’, also ‘product’ for WooCommerce Product, or any other custom post type you choose.

Step 2/2 – displaying a value to each post

Show an Advanced Custom Field Value

add_action( 'manage_post_posts_custom_column', function ( $column_name, $post_id ) 
{
    if ( $column_name == 'visits')
	{
		echo get_field("visits",$post_id);
	}
}, 10, 2 );

 

Show a Post Meta Value

add_action( 'manage_post_posts_custom_column', function ( $column_name, $post_id ) 
{
    if ( $column_name == 'visits')
	{
		echo get_post_meta($post_id, "visits", true);
	}
}, 10, 2 );

 

Hiding/Removing Un-needed Columns

Know that you can always use the Screen Options to hide unwanted columns, but that will only affect your user and not others.

 

The Solution

Adding a filter on manage_post_posts_columns, using the ‘unset‘ function calls to remove the unwanted columns and that’s it!

add_filter('manage_post_posts_columns', function ( $columns ) 
{
	// Here we 'unset' unwanted columns
	unset($columns['author']);
	unset($columns['categories']);
	unset($columns['tags']);
	
	// Here we add a new column
    if( is_array( $columns ) && ! isset( $columns['visits'] ) )
        $columns['visits'] = 'Total Visits';     
	
	// The 'return' call returns back the new chages we made
    return $columns;
} );

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

We are sorry that this post was not useful for you!

Let us improve this post!