How To Create a Custom Post Type in WordPress

“Custom Post Type” is one of the most powerful WordPress features. By default WordPress supports two post types: post and page.

There are many plugins for creating (and managing) custom post types. However, you can use code to create them.

The “Movie” custom post type

As an example, to create a custom post type for movies, the following code is usually enough:

function add_post_types()
{
    // Movie Post Type
    register_post_type('movie', [
        'public'          => true,
        'show_in_rest'    => true,
        'supports'        => ['title', 'editor', 'excerpt'],
        'labels'          => [
            'name'          => 'Movies',
            'add_new_item'  => 'Add New Movie',
            'edit_item'     => 'Edit Movie',
            'all_items'     => 'All Movies',
            'singular_name' => 'Movie'
        ],
        'rewrite'         => ['slug' => 'movies'],
        'has_archive'     => true,
        'menu_icon' => 'dashicons-format-video'
    ]);
}

add_action('init', 'add_post_types');

Remark: even better, use internationalization in labels, eg
'name' => __('Movies', 'yourtextdomain') etc

Where to put the code?

You may put the code in your function.php. However, if you change the theme, you will lose the relevant functionality.

So, the preferred solution is to use the “Must Use Plugins“. Just create a file (eg my-post-types.php) in the folder wp-content/mu-plugins and put the above code.

Customization

There are many options available for customization. Read register_post_type documentation.

Regarding menu icons, see Dashicons (the official icon font of the WordPress admin).

Of course, you can register more than one custom post type.

Result

Custom post type

Your comments are welcome!

MediSign - EHR for Small Medical Practices

EHR for Small Medical Practices

Medical records. Appointments. Electronic invoicing.

$9/month