0
0
WordpressHow-ToBeginner · 4 min read

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() into init can 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' => true will hide the post type from the admin menu and front-end.
  • Missing 'show_in_rest' => true disables 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

OptionDescriptionExample Value
labelsArray of names shown in admin UI{'name' => 'Books', 'singular_name' => 'Book'}
publicMakes post type visible in admin and front-endtrue
has_archiveEnables archive page for post typetrue
supportsFeatures supported like title, editor, thumbnail['title', 'editor', 'thumbnail']
show_in_restEnables Gutenberg editor supporttrue
menu_iconIcon 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.