How to Register Custom Post Type in WordPress Easily
To register a custom post type in WordPress, use the
register_post_type() function inside a hook like init. This function takes a unique slug and an array of options to define the post type's behavior and labels.Syntax
The register_post_type() function requires two main arguments: a unique post type slug (a string) and an array of arguments that define labels, visibility, and features.
$post_type: A unique string identifier for your post type (e.g., 'book').$args: An array of settings like labels, public visibility, supported features, and more.
This function is usually called inside the init action hook to ensure WordPress is ready.
php
add_action('init', function() { register_post_type('your_post_type_slug', [ 'labels' => [ 'name' => 'Your Post Types', 'singular_name' => 'Your Post Type' ], 'public' => true, 'has_archive' => true, 'supports' => ['title', 'editor', 'thumbnail'] ]); });
Example
This example registers a custom post type called Book with basic features like title, editor, and thumbnail support. It will appear in the WordPress admin menu and have its own archive page.
php
<?php add_action('init', function() { register_post_type('book', [ 'labels' => [ 'name' => 'Books', 'singular_name' => 'Book', 'add_new_item' => 'Add New Book', 'edit_item' => 'Edit Book', 'new_item' => 'New Book', 'view_item' => 'View Book', 'search_items' => 'Search Books', 'not_found' => 'No books found', 'not_found_in_trash' => 'No books found in Trash' ], 'public' => true, 'has_archive' => true, 'menu_icon' => 'dashicons-book', 'supports' => ['title', 'editor', 'thumbnail'], 'show_in_rest' => true ]); });
Output
A new 'Books' menu appears in the WordPress admin sidebar, allowing you to add, edit, and manage book posts with title, content editor, and featured image support.
Common Pitfalls
- Forgetting to hook
register_post_type()intoinitcan cause the post type not to register properly. - Using a post type slug longer than 20 characters or with spaces can cause errors.
- Not setting
'public' => truewill hide the post type from the admin menu and front-end. - Missing
'show_in_rest' => truedisables Gutenberg editor support.
php
<?php // Wrong: calling register_post_type outside init hook register_post_type('movie', [ 'public' => true ]); // Right: inside init hook add_action('init', function() { register_post_type('movie', [ 'public' => true ]); });
Quick Reference
| Option | Description | Example Value |
|---|---|---|
| labels | Array of names shown in admin UI | {'name' => 'Books', 'singular_name' => 'Book'} |
| public | Makes post type visible in admin and front-end | true |
| has_archive | Enables archive page for post type | true |
| supports | Features supported like title, editor, thumbnail | ['title', 'editor', 'thumbnail'] |
| show_in_rest | Enables Gutenberg editor support | true |
| menu_icon | Icon shown in admin menu | 'dashicons-book' |
Key Takeaways
Always register custom post types inside the 'init' hook using register_post_type().
Use a unique, short slug without spaces for your post type identifier.
Set 'public' => true to make the post type visible in admin and front-end.
Include 'show_in_rest' => true to enable the block editor (Gutenberg) support.
Define clear labels and supported features for better admin usability.