A custom post type gives your content its own admin menu, its own URLs and its own templates. Here is the shortest version that does all of that properly.
The plugin file
Create wp-content/plugins/books/books.php:
<?php
/**
* Plugin Name: Books
* Description: Adds a Book post type.
* Version: 1.0.0
*/
add_action( 'init', function () {
register_post_type( 'book', array(
'labels' => array(
'name' => __( 'Books', 'books' ),
'singular_name' => __( 'Book', 'books' ),
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true, // Needed for the block editor.
'menu_icon' => 'dashicons-book',
'rewrite' => array( 'slug' => 'books' ),
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
) );
} );
Activate it under Plugins, and a Books menu appears.
Flush the permalinks once
New post types need fresh rewrite rules. Visit Settings > Permalinks and press Save. Without this step, /books/ returns a 404.
You can also do it on activation:
register_activation_hook( __FILE__, function () {
// Register first, then flush, so the rules include the new type.
do_action( 'init' );
flush_rewrite_rules();
} );
What each option does
| Option | Why it matters |
|---|---|
show_in_rest |
Turns on the block editor and the REST API route. |
has_archive |
Gives you a list page at /books/. |
rewrite.slug |
Sets the URL base for single books. |
supports |
Chooses which editor panels appear. |
Templates
WordPress looks for single-book.php and archive-book.php in your theme. In a block theme, add templates/single-book.html instead.
Keep the post type in a plugin, not in the theme. If the theme changes, your content stays reachable.
