To create a custom post type in WordPress you call register_post_type() on the init hook with a key, a labels array and a set of arguments that decide where the type shows up. That is a dozen lines. What actually breaks on real sites is everything around it: a type registered before init fires, a key that collides with a reserved name, labels that leak the word “Post” into the editor, a REST route that 404s, and permalinks that only start resolving after somebody remembers to flush rewrite rules.
The six checks below are the ones worth running against a site that already ships a custom post type, or against the one you are about to. Each check tells you how to detect the failure with WP-CLI or the browser, then how to fix it in code. The running example is a book type with a [book_list] shortcode.
You need WordPress 6.7 or later, WP-CLI, and write access to wp-content/plugins. The reference plugin is a single file at wp-content/plugins/book-cpt/book-cpt.php. Activate it, then work through the checks.
<?php
/**
* Plugin Name: Book Library
* Description: Registers the book custom post type and a [book_list] shortcode.
* Version: 1.0.0
* Requires PHP: 7.4
*/
defined( 'ABSPATH' ) || exit;
/**
* Registers the book post type on init.
*/
function wptb_register_book_post_type() {
$labels = array(
'name' => _x( 'Books', 'post type general name', 'wptb' ),
'singular_name' => _x( 'Book', 'post type singular name', 'wptb' ),
'menu_name' => _x( 'Books', 'admin menu', 'wptb' ),
'all_items' => __( 'All Books', 'wptb' ),
'add_new' => __( 'Add New', 'wptb' ),
'add_new_item' => __( 'Add New Book', 'wptb' ),
'edit_item' => __( 'Edit Book', 'wptb' ),
'new_item' => __( 'New Book', 'wptb' ),
'view_item' => __( 'View Book', 'wptb' ),
'view_items' => __( 'View Books', 'wptb' ),
'search_items' => __( 'Search Books', 'wptb' ),
'not_found' => __( 'No books found.', 'wptb' ),
'not_found_in_trash' => __( 'No books found in Trash.', 'wptb' ),
'featured_image' => __( 'Cover image', 'wptb' ),
'set_featured_image' => __( 'Set cover image', 'wptb' ),
'remove_featured_image' => __( 'Remove cover image', 'wptb' ),
'use_featured_image' => __( 'Use as cover image', 'wptb' ),
'archives' => __( 'Book archives', 'wptb' ),
'item_published' => __( 'Book published.', 'wptb' ),
'item_updated' => __( 'Book updated.', 'wptb' ),
);
$args = array(
'labels' => $labels,
'description' => __( 'Catalogue entries for published books.', 'wptb' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'rest_base' => 'books',
'menu_icon' => 'dashicons-book-alt',
'menu_position' => 21,
'has_archive' => 'books',
'rewrite' => array(
'slug' => 'books',
'with_front' => false,
),
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'author', 'revisions', 'custom-fields' ),
'taxonomies' => array( 'category', 'post_tag' ),
'capability_type' => 'post',
'map_meta_cap' => true,
);
register_post_type( 'book', $args );
}
add_action( 'init', 'wptb_register_book_post_type' );
/**
* Writes the book rewrite rules once, when the plugin is activated.
*/
function wptb_book_activate() {
wptb_register_book_post_type();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'wptb_book_activate' );
/**
* Drops the book rules again when the plugin is deactivated.
*/
function wptb_book_deactivate() {
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'wptb_book_deactivate' );
/**
* [book_list] — prints the most recent books.
*/
function wptb_book_list_shortcode( $atts ) {
$atts = shortcode_atts(
array( 'count' => 5 ),
$atts,
'book_list'
);
$books = new WP_Query(
array(
'post_type' => 'book',
'post_status' => 'publish',
'posts_per_page' => absint( $atts['count'] ),
'ignore_sticky_posts' => true,
'no_found_rows' => true,
)
);
if ( ! $books->have_posts() ) {
return '<p class="book-list-empty">' . esc_html__( 'No books yet.', 'wptb' ) . '</p>';
}
$items = '';
foreach ( $books->posts as $book ) {
$items .= sprintf(
'<li><a href="%1$s">%2$s</a></li>',
esc_url( get_permalink( $book ) ),
esc_html( get_the_title( $book ) )
);
}
return '<h2>' . esc_html__( 'Latest books', 'wptb' ) . '</h2>'
. '<ul class="book-list">' . $items . '</ul>';
}
add_shortcode( 'book_list', 'wptb_book_list_shortcode' );
That file is the answer key for the rest of the article. Everything below is either a way to prove one of its decisions is wrong on your site, or the fix when it is.
Check 1: Is the post type registered on init, or has it been fired too early?
Detect. WordPress does not warn you about an early call, so check the code. Find every call and confirm each one sits inside a function hooked to init:
grep -rn "register_post_type(" wp-content/plugins wp-content/mu-plugins wp-content/themes
A call at the top level of a plugin file, or on a hook that fires before init, runs before WordPress has finished setting up rewrite rules and translations. The usual symptoms are permalinks that 404 and labels that stay in English on a translated site. The registry check in the next section shows what actually ended up registered.
Fix. Move the call into a named function and hook it to init. Do not use plugins_loaded, after_setup_theme, or top-level code in the plugin file.
// Too early: this runs while plugins are still loading.
register_post_type( 'book', $args );
// Correct: a named callback on init, priority 10.
function wptb_register_book_post_type() {
$args = array( /* ... */ );
register_post_type( 'book', $args );
}
add_action( 'init', 'wptb_register_book_post_type' );
Rewrite rules, the REST server and the block editor’s post type schema are all assembled after init. A type registered later is invisible to them, so you get a menu entry that works but no permalinks and no REST route. Keeping the registration on init at the default priority also means another plugin can hook init at priority 11 and still see your type.
Check 2: Does the post type key survive the 20-character and reserved-name rules?
Detect. register_post_type() returns a WP_Error instead of the object when the key is empty or longer than 20 characters. Ask it directly:
# What happens with a key that breaks the rules?
wp eval 'print_r( register_post_type( "a-very-long-post-type-key", array() ) );'
# Every public post type, with its REST base.
wp eval 'foreach ( get_post_types( array( "public" => true ), "objects" ) as $pt ) { printf( "%-22s rest=%s\n", $pt->name, $pt->show_in_rest ? $pt->rest_base : "off" ); }'
The first command prints a WP_Error with the code post_type_length_invalid. Reserved keys are a different trap, because WordPress does not reject them: registering post, page, attachment, revision, wp_block, wp_template or wp_navigation replaces the settings of the built-in type, which breaks core screens in ways that are hard to trace back.
Fix. Keep the key lowercase, 20 characters or fewer, and prefix it if the plugin might be installed next to another one that also wants book. Underscores are safer than hyphens because the key ends up in query vars, body classes and capability names. If you need a longer public-facing name, that belongs in rewrite['slug'], not in the key.
A failed registration is silent unless WP_DEBUG is on or you check the return value. The menu item never appears, and the only clue is a missing type, which is easy to blame on caching.
Check 3: Do the labels read like English on every screen?
Detect. Open the post type’s list table and press Add New. If the button says “Add New Post”, the editor heading says “Edit Post”, or the featured image box says “Featured image” instead of “Cover image”, the labels array is incomplete. The same information is available from the command line:
wp eval 'print_r( get_post_type_object( "book" )->labels );'
Fix. Supply the labels that WordPress does not derive well. name and singular_name cover most of the admin, but menu_name, all_items, add_new_item, edit_item, not_found and the four featured-image strings are worth setting explicitly. Wrap every string in _x() or __() with the same text domain as the plugin.

Labels are the only part of the post type an editor ever sees. A type called “Book” whose buttons say “Post” reads as a bug, and translators cannot fix it later if the strings were never passed through a translation function.
Check 4: Is show_in_rest on so the block editor and REST API can see the type?
Detect. Two quick tests. First, the registry:
wp eval 'var_dump( get_post_type_object( "book" )->show_in_rest );'
Second, the route. Request /wp-json/wp/v2/books in a browser. A rest_no_route error means the type is not exposed. On the editing screen the symptom looks different: the post type falls back to the classic editor even though supports includes editor.
Fix. Set 'show_in_rest' => true and, if you want a cleaner URL than the post type key, 'rest_base' => 'books'. The default controller is WP_REST_Posts_Controller, which is what you want unless the type needs custom fields with their own schema.
Without show_in_rest the type is invisible to the block editor and to any headless front end. It also blocks register_post_meta() with show_in_rest, so custom fields cannot be edited in the block editor sidebar.
Check 5: Are rewrite rules flushed on activation only?
Detect. Publish a book, then open its permalink. A 404 while the post exists in the database is the classic symptom. Confirm with:
wp rewrite list --match=/books/
wp option get rewrite_rules | grep -c books
If the first command lists nothing and the second prints 0, the rules for the type were never generated.
Fix. Flush once, when the plugin is activated, not on every request. The activation callback has to register the type first, because flush_rewrite_rules() rebuilds the rules from whatever is registered at that moment.
register_activation_hook( __FILE__, 'wptb_book_activate' );
function wptb_book_activate() {
// The type has to exist before the rules can be generated for it.
wptb_register_book_post_type();
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'wptb_book_deactivate' );
function wptb_book_deactivate() {
flush_rewrite_rules();
}
For a type registered from a must-use plugin there is no activation hook, so run wp rewrite flush --hard once after deploying and again after any change to rewrite['slug'] or has_archive.
flush_rewrite_rules() writes the rewrite_rules option. Calling it on init means a database write on every page load, and on a busy site that is a genuine performance problem. It also masks the real bug: if the rules are rebuilt constantly, a slug collision with a page or another post type never shows up until the flush is removed.
Check 6: Does a WP_Query or shortcode actually return the new posts?
Detect. Run the query outside the theme:
wp eval '
$q = new WP_Query( array( "post_type" => "book", "posts_per_page" => 5 ) );
printf( "found: %d\n", $q->found_posts );
foreach ( $q->posts as $p ) {
printf( " - %s -> %s\n", get_the_title( $p ), get_permalink( $p ) );
}
'
found: 0 with books in the database almost always means the query is missing 'post_type' => 'book', or is using query_posts() and clobbering the main query. A shortcode that prints “No books yet.” on a page with published books has the same cause.
Fix. Use a secondary WP_Query with an explicit post_type, post_status and no_found_rows when you do not need pagination. The [book_list] shortcode in the reference plugin does exactly that, and it escapes the permalink and the title on output rather than trusting them.
Run the whole registration through one script to see the state of all six checks at once:
Registration state of the book post type plus the posts the shortcode will list (test site: WordPress 7.1.3, PHP 8.2):
book post type: registered
show_in_rest: yes
rest_base: books
rewrite slug: books
has_archive: 'books'
menu label: Books
books found: 2
- Salt and Signal (http://localhost/books/salt-and-signal/)
- The Quiet Cartographer (http://localhost/books/the-quiet-cartographer/)
Then load the page that holds the shortcode. The heading and the book titles come from the query, so if they render, the type, the rewrite rules and the query are all working together:
![The [book_list] shortcode rendering books queried from the new post type (test site: WordPress 7.1.3, PHP 8.2)](https://i0.wp.com/wptutshub.com/wp-content/uploads/2026/11/create-custom-post-type-wordpress-test-2.png?fit=738%2C519&ssl=1)
A registered post type that no template queries is dead weight. The shortcode is the cheapest end-to-end proof: it exercises registration, the database, the query and the permalink in a single request.
To expose the new type’s custom fields to the block editor and the REST API, see adding custom post meta to the REST API with register_rest_field. For front-end layouts that pull from it, custom block patterns with dynamic data is the next step.
Once the six checks pass, the next thing worth doing is giving the type its own archive template. In a block theme, add archive-book.html to the theme’s templates folder or create it in the Site Editor; in a classic theme, add archive-book.php. The archive then stops borrowing the generic post layout.
Leave a Reply