Add Custom Column in Admin Post List in WordPress
In this post I’ll show you how to add Custom Columns to WordPress Posts Management screen. Also I will show how to make those Columns Sortable.
To add Columns in Posts Page in Admin Screen, add filter “manage_posts_columns” and action “manage_posts_custom_column” in functions.php of your theme.
// register custom columns add_filter( 'manage_posts_columns', 'custom_manage_post_column_test' ); function custom_manage_post_column_test( $columns ) { $columns['test'] = 'Test Column'; return $columns; } // values to display in custom column add_action( 'manage_posts_custom_column', 'custom_manage_post_column_test_data', 10, 2 ); function custom_manage_post_column_test_data( $column_name, $post_id ) { if( $column_name == 'test' ) { $data = 'get some data here'; echo '<strong>' . $data . '</strong>'; } }
Now your Admin screen will include a column with name “Test Column”.
To make it sortable, add below filters in functions.php of your theme.
add_filter( 'manage_edit-post_sortable_columns', 'custom_manage_post_column_test_sorting' ); function custom_manage_post_column_test_sorting( $columns ) { $columns['test'] = 'test'; return $columns; } add_filter( 'request', 'custom_manage_post_column_test_orderby' ); function custom_manage_post_column_test_orderby( $vars ) { if ( isset( $vars['orderby'] ) && 'test' == $vars['orderby'] ) { // if it is some post meta variable then use this, otherwise modify query accordingly $vars = array_merge( $vars, array( 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num' // if text, then use meta_value instead of meta_value_num ) ); } return $vars; }