
How to add Columns to Admin Posts Listing
- December 9, 2015
- Leave a comment
WordPress back-end admin listing by default comes up with Title, Author, Categories, Tags and Date columns. Sometimes, you may want to add more columns to get maximum information on post listing page.
By using a hook manage_edit-post_columns, you can extend the post listing. For instance, you want to extend the listing for ID and Image column. Following is the sample code to add more columns to the existing columns in post listing table:
1 2 3 4 5 6 |
add_filter('manage_edit-post_columns', 'add_new_post_columns'); function add_new_gallery_columns($post_columns) { $post_columns['id'] = __('ID'); $post_columns['images'] = __('Images'); return $post_columns; } |
Following code is to populate data for these columns to display:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
add_action('manage_gallery_posts_custom_column', 'manage_gallery_columns', 10, 2); function manage_gallery_columns($column_name, $id) { global $wpdb; switch ($column_name) { case 'id': echo $id; break; case 'images': $num_images = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_parent = %d;", $id)); echo $num_images; break; default: break; } // end switch } |
User Comments