Books Post Type With Genres and Authors
Registers a "Books" post type with a Genre category taxonomy and a tag-style Book Author taxonomy, for publishers, libraries and reading lists.
Opens WPIDE on
Code
// Names used in labels and URLs: edit to suit.
$wpide_type = array(
'name' => 'book', // Internal name: lowercase, max 20 characters, never change it once you have content.
'singular' => 'Book',
'plural' => 'Books',
'slug' => 'books',
);
add_action( 'init', function () use ( $wpide_type ) {
$s = $wpide_type['singular'];
$p = $wpide_type['plural'];
register_post_type( $wpide_type['name'], array(
'labels' => array(
'name' => $p,
'singular_name' => $s,
'menu_name' => $p,
'add_new_item' => "Add New $s",
'edit_item' => "Edit $s",
'new_item' => "New $s",
'view_item' => "View $s",
'view_items' => "View $p",
'all_items' => "All $p",
'search_items' => "Search $p",
'not_found' => 'Nothing found.',
'not_found_in_trash' => 'Nothing found in Trash.',
'archives' => "$s Archives",
'attributes' => "$s Attributes",
'insert_into_item' => "Insert into $s",
'uploaded_to_this_item' => "Uploaded to this $s",
'item_published' => "$s published.",
'item_updated' => "$s updated.",
),
'public' => true,
'has_archive' => $wpide_type['slug'],
'rewrite' => array( 'slug' => $wpide_type['slug'], 'with_front' => false ),
'show_in_rest' => true,
'menu_icon' => 'dashicons-book-alt',
'menu_position' => 30,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' ),
) );
register_taxonomy( 'genre', $wpide_type['name'], array(
'labels' => array(
'name' => 'Genres',
'singular_name' => 'Genre',
'menu_name' => 'Genres',
'all_items' => 'All Genres',
'edit_item' => 'Edit Genre',
'view_item' => 'View Genre',
'update_item' => 'Update Genre',
'add_new_item' => 'Add New Genre',
'new_item_name' => 'New Genre Name',
'search_items' => 'Search Genres',
'not_found' => 'No genres found.',
'parent_item' => 'Parent Genre',
),
'hierarchical' => true,
'public' => true,
'rewrite' => array( 'slug' => 'genre', 'with_front' => false ),
'show_admin_column' => true,
'show_in_rest' => true,
) );
register_taxonomy( 'book_author', $wpide_type['name'], array(
'labels' => array(
'name' => 'Book Authors',
'singular_name' => 'Book Author',
'menu_name' => 'Book Authors',
'all_items' => 'All Book Authors',
'edit_item' => 'Edit Book Author',
'view_item' => 'View Book Author',
'update_item' => 'Update Book Author',
'add_new_item' => 'Add New Book Author',
'new_item_name' => 'New Book Author Name',
'search_items' => 'Search Book Authors',
'not_found' => 'No book authors found.',
'popular_items' => 'Popular Book Authors',
'separate_items_with_commas' => 'Separate book authors with commas',
'choose_from_most_used' => 'Choose from the most used book authors',
),
'hierarchical' => false,
'public' => true,
'rewrite' => array( 'slug' => 'book-author', 'with_front' => false ),
'show_admin_column' => true,
'show_in_rest' => true,
) );
} );
Notes
Book authors work like tags, so a book can have several.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.