0
0
WordpressHow-ToBeginner · 4 min read

How to Create Custom Post Type in WordPress Easily

To create a custom post type in WordPress, use the register_post_type function inside a hook like init. This function lets you define the post type's name, labels, and behavior so WordPress treats it like posts or pages but customized.
📐

Syntax

The register_post_type function registers a new post type in WordPress. It requires two main arguments: the post type key (a unique string) and an array of arguments to define labels and settings.

  • post_type: A unique slug for your post type (e.g., 'book').
  • args: An array that sets labels, visibility, supports, and other options.
php
register_post_type( string $post_type, array $args = array() );
💻

Example

This example creates a custom post type called "Book" that supports title, editor, and thumbnail. It appears in the WordPress admin menu and is public.

php
<?php
function create_book_post_type() {
    $labels = array(
        'name' => 'Books',
        'singular_name' => 'Book',
        'menu_name' => 'Books',
        'name_admin_bar' => 'Book',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Book',
        'new_item' => 'New Book',
        'edit_item' => 'Edit Book',
        'view_item' => 'View Book',
        'all_items' => 'All Books',
        'search_items' => 'Search Books',
        'not_found' => 'No books found.',
        'not_found_in_trash' => 'No books found in Trash.',
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-book',
        'supports' => array('title', 'editor', 'thumbnail'),
        'show_in_rest' => true,
    );

    register_post_type('book', $args);
}
add_action('init', 'create_book_post_type');
Output
A new "Books" menu appears in WordPress admin where you can add, edit, and view Book posts.
⚠️

Common Pitfalls

Common mistakes when creating custom post types include:

  • Using a post type key longer than 20 characters or with spaces (must be a short slug).
  • Not hooking register_post_type to init, causing it not to register properly.
  • Forgetting to set 'public' => true if you want the post type visible on the front end.
  • Not adding 'show_in_rest' => true if you want Gutenberg editor support.
php
<?php
// Wrong: calling register_post_type outside init hook
register_post_type('movie', array('public' => true));

// Right: inside init hook
add_action('init', function() {
    register_post_type('movie', array('public' => true));
});
📊

Quick Reference

ArgumentDescriptionExample
post_typeUnique slug for the post type'book'
labelsArray of names shown in admin UI['name' => 'Books', 'singular_name' => 'Book']
publicMakes post type visible on front endtrue
has_archiveEnables archive page for post typetrue
supportsFeatures supported like title, editor['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 to ensure proper loading.
Use a short, unique slug (max 20 characters, no spaces) as the post type key.
Set 'public' => true to make the post type visible on the site front end.
Add 'show_in_rest' => true to enable Gutenberg editor support.
Customize labels and supports array to control admin UI and features.