In many situations you may need to add custom columns to the default table provided in the WordPress ‘All Post’ and ‘All Page’ section.
So say you want to add a column that displays the value of a meta field ‘page_views’.
To add new columns to the post listing page put the following code in your active theme’s function.php file.
add_filter('manage_post_posts_columns', 'display_new_column'); function display_new_column( $defaults ) { $defaults['page_views'] = 'Page Views'; return $defaults; }
The above code will add the new column to the default table. Now its time to add content to this new column. The below code will output the value of each row for the newly created column.
add_action( 'manage_post_posts_custom_column', 'new_column_content', 10, 2); function new_column_content( $column_name, $post_id ) { if ($column_name == 'page_views') { echo get_post_meta($post_id, 'page_views', true); } }
If you want to remove any default column say the commments column use the below code
add_filter('manage_post_posts_columns', 'remove_unwanted_columns'); function remove_unwanted_columns($defaults) { unset($defaults['comments']); return $defaults; }
The main keyword in the above code is the filter manage_post_posts_columns which helps us in adding custom columns to the post’s table
The above codes are for Post’s listing page. For Page’s listing page just replace manage_posts_posts_columns with manage_page_posts_columns and manage_post_posts_custom_column with manage_page_posts_custom_column.