wp admin - Adding custom columns to custom post types - WordPress Development Stack Exchange

PHOTO EMBED

Fri Jul 29 2022 14:41:01 GMT+0000 (Coordinated Universal Time)

Saved by @kalehm

// 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) {

    $columns['book_author'] = __( 'Sub-Author', 'your_text_domain' );
    $columns['publisher'] = __( 'Publisher', 'your_text_domain' );

    return $columns;
//	To add after one column  
//        $n_columns = array();
//        foreach($columns as $key => $value) {
//            if ($key=='title'){
//                $n_columns[$key] = $value;
//                $n_columns['book_author'] = __( 'Author', 'your_text_domain' );
//                $n_columns['publisher'] = __( 'Publisher', 'your_text_domain' );
//            }
//            $n_columns[$key] = $value;
//        }
//        return $n_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;

    }
}
content_copyCOPY

https://wordpress.stackexchange.com/questions/253640/adding-custom-columns-to-custom-post-types