27

I've done this previously but I've forgotten the name of the hook, and can't find it anywhere...

What I'm trying to do is add some custom columns in the listing of a custom post type in the admin.

For example, in the admin, click on articles, I want to add custom column there.

54

The hooks to create custom columns and their associated data for a custom post type are manage_{$post_type}_posts_columns and manage_{$post_type}_posts_custom_column respectively, where {$post_type} is the name of the custom post type.

This example from the documentation removes the author column and adds a taxonomy and meta data column:

// Add the custom columns to the book post type:
add_filter( 'manage_book_posts_columns', 'set_custom_edit_book_columns' );
function set_custom_edit_book_columns($columns) {
    unset( $columns['author'] );
    $columns['book_author'] = __( 'Author', 'your_text_domain' );
    $columns['publisher'] = __( 'Publisher', 'your_text_domain' );

    return $columns;
}

// Add the data to the custom columns for the book post type:
add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
function custom_book_column( $column, $post_id ) {
    switch ( $column ) {

        case 'book_author' :
            $terms = get_the_term_list( $post_id , 'book_author' , '' , ',' , '' );
            if ( is_string( $terms ) )
                echo $terms;
            else
                _e( 'Unable to get author(s)', 'your_text_domain' );
            break;

        case 'publisher' :
            echo get_post_meta( $post_id , 'publisher' , true ); 
            break;

    }
}
  • 1
    Thank you for this hook!!! This is brilliant!! There's no clear documentation on stackoverflow!!! :D Cheers mate – Rodrigo Zuluaga May 30 at 2:12
  • isn't there any option to set the column number? like column_index[2]. Because the custom_column appears at the end of the column. – Dilip Gupta Jun 21 at 8:52
  • @DilipGupta $columns is an array, you can reorder before you return it – fogx Jul 16 at 10:39
0

I'm not sure if it default custom meta data that you want to show as columns, but you could consider to use this free plugin that allows you to add columns to display custom fields. https://wordpress.org/plugins/codepress-admin-columns/

The pro version even allows you to add filtering, sorting and inline edit to those columns.

  • DO NOT USE! Will break your site: PHP Fatal error: Cannot redeclare AC() (previously declared in [redacted]\\wp-content\\themes\[redacted]\\functions.php:628) in [redacted]\\wp-content\\plugins\\codepress-admin-columns\\api.php on line 9 – Peter Kionga-Kamau Nov 6 '18 at 20:17
  • @PeterKionga-Kamau this is a compatibility-issue with your specific theme. The error you posted will not appear with a default WP theme (such as twentytwenty); I also use the plugin with the Divi Theme and find it quite useful – Philipp Oct 8 at 19:34
  • Isn't there some kind of namespacing or use of less generic function names that could be used to avoid this? – Peter Kionga-Kamau Oct 9 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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