How does one customize the table of listings for a custom post type?

For example, if I have a Custom Post Type called “Video” and I want to show the length of that video in the main (edit.php) table listing, how can I manipulated the columns shown in that table?

Related posts

Leave a Reply

1 comment

  1. it’s similar to adding columns to regular posts, except the filter and action are slightly different: manage_edit-{$post_type}_columns and manage_{$post_type}_posts_custom_column

    function wpse27787_add_video_column( $columns ){
        // add a new column to array of columns
        // can also unset columns here to remove them
        $columns['length'] = __('Length');
        return $columns;
    }
    
    function wpse27787_manage_video_columns( $column_name, $id ){
        // if this is our custom column, fetch whatever data we want to output
        if( $column_name == 'length' ):
            // get your video length here using this post's $id and
            echo $this_length;
        endif;
    }   
    
    function wpse27787_init() {
        // add our filter and action on admin_init
        add_filter( 'manage_edit-video_columns', 'wpse27787_add_video_column' );
        add_action( 'manage_video_posts_custom_column', 'wpse27787_manage_video_columns', 10, 2 );
    }
    add_action( 'admin_init' , 'wpse27787_init' );
    

    EDIT –

    if you want to reorder the columns, create a new array with the values from the original, adding your column in the desired position:

    function wpse27787_add_video_column( $columns ){
    
        foreach( $columns as $key => $val):
            $reordered_columns[$key] = $val;
            if( $key == 'title' ):
                // add our custom column after title
                $reordered_columns['length'] = __('Length');
            endif;
        endforeach;
    
        return $reordered_columns;
    }