In this article10 sections
Posts and pages are fine for a blog, but not for everything. A bookshop needs books, a restaurant needs menu items, a real estate site needs listings. A custom post type lets you create a new kind of content with its own menu, its own editing screen and its own URL structure.
This guide shows how to create a custom post type and add a custom field to it, using only code. No extra plugin is required.
Step 1: Register the custom post type
Add this to a plugin (the best place) or your child theme’s functions.php:
function bk_register_book_cpt() {
register_post_type( 'book', array(
'labels' => array(
'name' => 'Books',
'singular_name' => 'Book',
'add_new_item' => 'Add New Book',
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'menu_icon' => 'dashicons-book',
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
'rewrite' => array( 'slug' => 'books' ),
) );
}
add_action( 'init', 'bk_register_book_cpt' );
What the options do:
publicmakes it visible on the site and in the admin.has_archivecreates an archive page at/books/.show_in_restturns on the block editor and the REST API for the type. Without it, you get the classic editor.supportschooses which editing boxes appear.rewritesets the URL slug.
After adding it, go to Settings → Permalinks and click Save Changes once. This refreshes the rewrite rules, so /books/ does not show a 404. Do not flush rules on every page load from code, as it slows the site.

Step 2: Add a custom field with a meta box
A custom field stores extra information about a post, such as a book’s author. The classic way is a meta box:
function bk_add_meta_box() {
add_meta_box( 'bk_details', 'Book details', 'bk_render_meta_box', 'book' );
}
add_action( 'add_meta_boxes', 'bk_add_meta_box' );
function bk_render_meta_box( $post ) {
wp_nonce_field( 'bk_save_meta', 'bk_nonce' );
$author = get_post_meta( $post->ID, '_bk_author', true );
echo '<label for="bk_author">Author</label> ';
echo '<input type="text" id="bk_author" name="bk_author" value="' . esc_attr( $author ) . '" />';
}
Open Books → Add New. You will see a “Book details” box under the editor.
Step 3: Save the field safely
Saving is where security mistakes happen. Check the nonce, ignore autosaves, check permissions, then sanitize:
function bk_save_meta( $post_id ) {
if ( ! isset( $_POST['bk_nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['bk_nonce'] ) ), 'bk_save_meta' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( isset( $_POST['bk_author'] ) ) {
update_post_meta( $post_id, '_bk_author', sanitize_text_field( wp_unslash( $_POST['bk_author'] ) ) );
}
}
add_action( 'save_post_book', 'bk_save_meta' );
The underscore at the start of _bk_author hides the field from the default Custom Fields box, since it is managed by your own box.
Step 4: Show the field on the front end
In your theme’s template, for example single-book.php:
$author = get_post_meta( get_the_ID(), '_bk_author', true );
if ( $author ) {
echo '<p>By ' . esc_html( $author ) . '</p>';
}
Always escape output with esc_html(), even though you sanitized on save.
Make the field available to the block editor and REST API
If you want the field in the REST API or editable from a block, register it:
function bk_register_meta() {
register_post_meta( 'book', '_bk_author', array(
'type' => 'string',
'single' => true,
'show_in_rest' => true,
'auth_callback' => function () {
return current_user_can( 'edit_posts' );
},
) );
}
add_action( 'init', 'bk_register_meta' );
When to use a plugin instead
Writing this by hand is great for learning and for small projects. If you have many fields, repeaters or a client who wants to edit field settings, a plugin such as Advanced Custom Fields saves time. Even then, understanding how it works underneath will help you debug problems.
Add a taxonomy too
Custom post types often need their own categories. Register one with register_taxonomy( 'genre', 'book', array( 'hierarchical' => true, 'show_in_rest' => true, 'label' => 'Genres' ) ); on the init hook.
Common mistakes
- 404 on the new URLs. Resave Settings → Permalinks.
- Post type slug too long. The key must be 20 characters or fewer, lowercase, with no spaces.
- Putting the code in a theme. If you switch themes, the content type disappears from the admin. Use a plugin.
- Skipping the nonce check. This leaves your save routine open to forged requests.
Frequently asked questions
Will my content be lost if I deactivate the plugin?
No. The posts stay in the database, but they will not be visible until the post type is registered again.
Can I change an existing post type into another?
Yes, but carefully. You need to update the post_type value in the database, and you should take a backup first.
Does a custom post type affect SEO?
Not negatively. Search engines treat them as normal pages. Make sure public is true and the type is included in your sitemap.
Summary
Register the post type on init, resave your permalinks, add fields with a meta box, and always use a nonce, a permission check and sanitizing when you save. Next, see how to build the plugin around it in our guide to creating a WordPress plugin from scratch.












