What is a custom post type?
WordPress comes with built-in content types. The two you know best are Posts and Pages. A custom post type is a new content type that you make yourself. Think of a school bag. One pocket holds pens and another holds books. A custom post type is a new pocket for one kind of content.
You can make one in two ways. A plugin is easier. Code gives you more control.
Method 1: Use a plugin
A plugin is a small add-on program for WordPress. Many free plugins can create post types. Custom Post Type UI is a popular one.
- Log in to your WordPress dashboard. The dashboard is your control room.
- Click Plugins, then Add New.
- Search for "Custom Post Type UI".
- Click Install Now, then Activate.
- Click the new CPT UI menu, then Add/Edit Post Types.
- Type a short name in Post Type Slug. Use small letters only, such as
book. - Type the plural name, such as "Books", and the single name, such as "Book".
- Click Add Post Type.
A new "Books" item now shows in your left menu. Click it to add entries.
Method 2: Use code
A child theme is a safe copy layer on top of your main theme. Your changes stay when the main theme updates.
- Open your child theme's
functions.phpfile. You can use the File Manager in your hosting control panel. - Add this code at the bottom of the file.
function hv_register_book_type() {
register_post_type('book', array(
'labels' => array(
'name' => 'Books',
'singular_name' => 'Book'
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'show_in_rest' => true
));
}
add_action('init', 'hv_register_book_type');
This code tells WordPress to create a public "Books" type with a title, a text editor and a picture box.
- Save the file.
- In the dashboard, click Settings, then Permalinks, then Save Changes. This refreshes your page links.
Check that it works
- Click Books in the left menu.
- Click Add New.
- Write a title and some text.
- Click Publish, then view the entry.
Quick recap
- A custom post type is a new kind of content in WordPress.
- A plugin is the easiest way to make one.
- Code in a child theme gives more control. Back up first.
- Save your Permalinks after you add a new type.