Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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;
}
share|improve this answer
    
Great info. Concise and flows right in. This will help in a lot of ways, thank you. –  JMichael Sep 6 '11 at 4:24
    
I notice the column shows at the end. Do the "10" and "2" arguments indicate placement/order of the column? –  JMichael Sep 6 '11 at 4:34
    
no, that's the priority of the function itself. it appears at the end because you're just adding an element to the end of the array and passing it back. if you want it in a specific order, create a new array with the keys/values from $columns, inserting your own column in the appropriate place and pass that back, see my edit above. –  Milo Sep 6 '11 at 5:15
    
Ah, perfectly explained. Thanks again, Sir. –  JMichael Sep 7 '11 at 9:11

Here is some code from scribu.net for registering a custom column and displaying it:

// Register the column
function price_column_register( $columns ) {
    $columns['price'] = __( 'Price', 'my-plugin' );

    return $columns;
}
add_filter( 'manage_edit-post_columns', 'price_column_register' );


// Display the column content
function price_column_display( $column_name, $post_id ) {
    if ( 'price' != $column_name )
        return;

    $price = get_post_meta($post_id, 'price', true);
    if ( !$price )
        $price = '<em>' . __( 'undefined', 'my-plugin' ) . '</em>';

    echo $price;
}
add_action( 'manage_posts_custom_column', 'price_column_display', 10, 2 );

This is just part of a tutorial on making custom columns that are sortable. Then there's a followup tutorial on how to handle sortable taxonomy columns.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.