Skip to content

Sample post: placeholder content to show the layout. It will be replaced by real writing.

How to register a custom post type in WordPress

A small, complete plugin that adds a "Book" post type with its own archive, block editor support and clean URLs.

By 1 min readWordPress

On this page

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.

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.

Sahadat Husain

Developer, Devsdemy

Builds WordPress plugins and web tools, and writes down what worked.

More to read