WordPress 3.1 Custom Columns in List View

March 15th, 2011
|

So this got me for a minute. You manage your custom columns in WordPress using add_filter.

First you have to add them:
add_filter('manage_posts_columns', 'add_new_columns');
function add_new_columns($columns) {
$columns['thumb'] = __('Thumb');
return $columns;
}

…etc.

Then you populate them (in my case, add post type to any column named ‘thumb’):
add_action( 'manage_posts_custom_column', 'manage_columns' );
function manage_columns($column_name, $post_id) {
switch ($column_name) {
case 'thumb':
if(has_post_thumbnail($post_id)){
echo get_the_post_thumbnail($post_id,array(50,50));
}else{
echo 'No Featured Image';
}
break;
default:
break;
}
}

This is what got me

If your post types are hierarchical, then WordPress considers them ‘pages’. If that is the case you have to use:

add_action( 'manage_pages_custom_column', 'manage_columns' );

This will add the action to all hierarchical post types (pages) instead of all non-hierarchical post types (posts). If you want to target your custom post types only, hierarchical or not, you can do so using:

add_action( 'manage_{custom-post-type}_posts_custom_column', 'manage_columns' );

I know we are moving fast, so… a quick rundown:

Global non-hierarchical post types (posts) use:
add_action( 'manage_posts_custom_column', 'manage_columns' );

Global hierarchical post types (pages) use:
add_action( 'manage_pages_custom_column', 'manage_columns' );

Specific post types hierarchical or non-hierarchical (custom post types) use:
add_action( 'manage_{custom-post-type}_posts_custom_column', 'manage_columns' );

Hope that saves someone a few minutes.

3 Comments:

  1. Thank you for posting this, it probably saved me a few hours, not minutes.

    I also wanted to add that if anyone receives a PHP warning about a missing argument (Warning: Missing argument 2 for my_manage_columns()) it can be resolved by adding priority and accepted_args parameters to the add_action call:

    add_action( ‘manage_posts_custom_column’, ‘my_manage_columns’, 10, 2 );

  2. Thanks Jonathan. I just referenced this and used your comment to help me out.

  3. Thanks for this! Your comment regarding hierarchical post types being treated as pages, thus using ‘manage_pages_custom_column’, just saved me a lot of time 🙂

Comments have been closed for this post. If you would like to ask a question or need help, please post in the forums.