One of the exciting new features in WordPress 3.0 is custom post types. We can create our own post types by using the register_post_type function.
If we enable UIs for our custom post type, we will get additional menu items on our WordPress dashboard similar to the Edit and Add New options for standard posts.
Here, we consider how to add new columns to the Edit custom post type screen.
1. Create a Custom Post Type
Creating a custom post type is surprisingly straight-forward and well documented in the WordPress Codex.
In this example, we create a custom post type called gallery.
$labels = array( 'name' => _x('Galleries', 'post type general name'), 'singular_name' => _x('Gallery', 'post type singular name'), 'add_new' => _x('Add New', 'gallery'), 'add_new_item' => __("Add New Gallery"), 'edit_item' => __("Edit Gallery"), 'new_item' => __("New Gallery"), 'view_item' => __("View Gallery"), 'search_items' => __("Search Gallery"), 'not_found' => __('No galleries found'), 'not_found_in_trash' => __('No galleries found in Trash'), 'parent_item_colon' => '' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title','thumbnail','excerpt') ); register_post_type('gallery',$args);
2. Add New Custom Post Type Columns
As shown in the screen-shot above, our custom post type Edit screen (called Gallery) starts off with four columns – checkbox, Title, Author, and Date.
To add new columns to our Edit screen, we want to hook into the manage_$pagename_columns filter. The $pagename of the Edit screen is edit-$post_type.
Therefore, in this example, –
- Edit page name = edit-gallery
- Add column filter hook = manage_edit-gallery_columns
Our add column function call looks like this –
// Add to admin_init function add_filter('manage_edit-gallery_columns', 'add_new_gallery_columns');
Our filter function accepts an array of column names, and returns our new column array once we are done.
function add_new_gallery_columns($gallery_columns) { $new_columns['cb'] = '<input type="checkbox" />'; $new_columns['id'] = __('ID'); $new_columns['title'] = _x('Gallery Name', 'column name'); $new_columns['images'] = __('Images'); $new_columns['author'] = __('Author'); $new_columns['categories'] = __('Categories'); $new_columns['tags'] = __('Tags'); $new_columns['date'] = _x('Date', 'column name'); return $new_columns; }
In the example above we fully replace the column array with our own entries. We can also just add columns by adding new elements into the existing $gallery_columns array. However, our added columns will only appear after the existing default columns.
The functions above will add new columns into our Edit Gallery screen which now looks like this –
If we want the column to be sortable, then we can use the manage_{$screen->id}_sortable_columns filter. Here is a good example for making sortable columns.
3. Render Our New Custom Post Type Columns
Note – In the screen-shot above, the ID and Images columns are empty because they are not standard WordPress post columns. Standard WordPress post columns include –
- ‘cb’ – Post checkbox.
- ‘date’ – Date when post was last modified.
- ‘title’ – Post title and common post actions including Edit, Quick Edit, Trash, and View.
- ‘categories’ – Post categories.
- ‘tags’ – Post tags.
- ‘comments’ – Number of post comments.
- ‘author’ – Post author.
To render our new columns, ‘id’ and ‘images’, we must hook into the manage_posts_custom_column action (in WordPress 3.0).
In WordPress 3.1, we want to hook into the manage_{$post_type}_posts_custom_column action. Since our example post type is gallery, we want to hook into manage_gallery_posts_custom_column.
// Add to admin_init function 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': // Get number of images in gallery $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 }
Our ‘id’ and ‘images’ columns will now be rendered with the proper values.
We Are Done!
Another interesting action hook in the Edit screen page is restrict_manage_posts which allows us to control which custom post objects we want to show on the page.
And just like that … we are done!
Andrea says
great tutorial!
i have a question: i want to let the user order post by a custom field value, insted of simply show the value of that custom field.
Thank you
ShibaShake says
Use the manage_{$screen->id}_sortable_columns filter. E.g., ‘manage_edit-gallery_sortable_columns’.
Nuwanda says
Ok, this sorta works for my custom post type.
But I have a custom taxomony instead of the normal wordpress categories.
How do I show the custom taxonomy that the post belongs to?
If my custom taxonomy is Cars, I’m using…
$new_columns[‘cars’] = __(‘Car’);
..but all I’m getting is the header and no taxonomy listed.
ShibaShake says
You need to fill in the column values yourself by hooking into the manage_{$post_type}_posts_custom_column action.
Check out section 3 in the article above.
Trewknowledge says
I am also having a problem displaying the right category. I have a custom post called “slider” so I wrote out
manage_slider_posts_custom_column
However all of the posts say “Uncategorized”. All of the posts have a “Slider Category” associated with it.
Any thoughts?
ShibaShake says
It is difficult to say without looking at the code.
First, I would just do an “echo blah” to see if your manage_slider_posts_custom_column function is being called. If the function is being called, then likely, there is some bug while extracting the category values from the slider objects.
Dan says
Hi, I just used part of this code to add thumbnails to my custom post types. I used get_the_post_thumbnail($post-ID,array(100,100)); in the switch part for a column I made called image. Worked an absolute treat.
Epic post, thanks.
Dan says
whoops, I meant $post->ID. Im always missing the “>” out, bad habit!
Adedoyin Kassem says
Thanks for this post, it has been very enlightening. However i have got just one challenge using this feature. I have a custom post type for events and i use this code (below) to create custom columns:
function add_new_events_columns($event_columns) {
$new_columns[‘cb’] = ”;
$new_columns[‘title’] = _x(‘Event Name’, ‘column name’);
$new_columns[‘location’] = _x(‘Event Location’, ‘column name’);
$new_columns[‘events_date’] = _x(‘Event Date’, ‘column name’);
$new_columns[‘date’] = _x(‘Date Posted’, ‘column name’);
return $new_columns;
}
The columns show perfectly well. However, when i tried to call out the content of the location meta box using this code:
function manage_events_columns($column_name, $id) {
global $wpdb;
switch ($column_name) {
case ‘location’:
$location_meta = get_post_meta($post->id, ‘location’, true);
echo $location_meta;
break;
}
}
I can’t seem to use this method to get the content of the meta boxes to show in the respective columns. Please, i need help on this.
ShibaShake says
In the code above you did not set $post. Try just using $id.
Matt says
Is it possible to add custom meta field values to the columns? I would like to set _lastname, _firstname as the title. Thanks Shibashake!
ShibaShake says
You should be able to display whatever you want by putting it into the manage_gallery_columns function.
Joe says
Just confirmed: everything is fine in 3.1 RC2 except that If the custom post type is registered as ‘hierarchical’ => true, then the column will not populate with the data.
Kcssm says
Yes I also got the same problem. The columns doesnot get populated when the `‘hierarchical’ => true` is set. Looking for a solution.
Thanks..
ShibaShake says
For hierarchical custom post types in WP 3.1 try using –
add_action(‘manage_pages_custom_column’, ‘manage_gallery_columns’, 10, 2);
or you can also use –
add_action(“manage_{$post->post_type}_posts_custom_column”, ‘manage_gallery_columns’, 10, 2);
alex chousmith says
in WP 3.0.4, this is also the solution to this problem.
basically, if your custom post type IS hierarchical, the action hook is “manage_pages_custom_column”. if you did not specificy “hierarchical” => true in the register_post_type , it is by default FALSE, and you can hook to custom columns via the “manage_posts_custom_column” as shown in this post
good times
Joe says
Seems like WordPress 3.1 RC2 is having issues echoing the value. Was fine on RC1. Anyone else notice?
Team Roster says
You you could edit the webpage name title
Add Custom Post Type Columns to something more catching for your webpage you write. I loved the post yet.