Custom post types enable you to create unique types of content for your website beyond posts and pages. They convert your WordPress blog into a robust content management system (CMS).
This article will show you how to create unique post types in WordPress.
What are custom post types?
Post types are used to differentiate between different types of content on your WordPress website. Pages and posts are both types of posts, but they fulfill specific responsibilities.
WordPress includes the following post types by default:
- Navigation Menu
- Page
- Post
- Revision
- Attachment
Custom post types allow you to create your own post types. These are helpful when creating content that does not follow the standard post or page format.
For example, if you run a website dedicated to music album reviews, you would likely choose to create a post type for album reviews. You could also create custom post types for testimonials, portfolios, and products.
You can add different custom fields and a unique category structure to custom post types.
Numerous popular WordPress plugins utilize custom post types to store and manage data on your WordPress website.
Before adding custom post types to your WordPress website, it’s important to consider your requirements.
How to create a custom post type in WordPress
Using a plugin
Using the plugin is the simplest way of creating a custom post type in WordPress. This technique is best for beginners because it’s straightforward and simple.
The first step is to install and activate the Custom Post Type UI plugin:
Head to Plugins > Add New in your WordPress dashboard, search for Custom Post Type UI plugin in the top-right search bar and click Install Now:
Once installed, click Activate to activate the plugin:
After activation, you must navigate to CPT UI > Add/Edit Post Types to create a new custom post type. You need to be on the ‘Add New Post Type‘ tab to do this.
You must first provide a slug for your custom post type, such as music. The system will use this slug in the URL and WordPress requests, so it can only be made up of letters and numbers. Then, you specify your custom post type’s plural and singular names.
Then, if you want, you can click the link that reads “Populate additional labels based on selected labels.” This will save you time by automatically populating the extra label fields below.
Scroll down to the ‘Additional Labels’ section now. If you did not click the automatic feature mentioned before – you should now provide a post-type description and modify labels.
These labels will appear throughout the WordPress user interface when you’re managing content in that particular post type.
The post-type settings come next. Here, you can configure various attributes for your post type, and each option includes a brief explanation of its function.
For example, you can decide not to make a post type hierarchical like pages or sort posts in reverse order of when they were written.
Below the general settings, you can choose which editing features this post type will support. Tick off the options you want to include.
To save and create your custom post type, click the Add Post Type button.
That’s it!
Creating a custom post type manually
If you ever deactivate your custom post types plugin, custom post types are lost, and your custom post type will become unregistered and inaccessible from the administration panel. This is why you might want to add your custom post type manually.
If you are working on a client’s site and do not want to install another plugin, you can manually create a custom post type by adding the code to your WordPress theme’s functions.php file or a site-specific plugin.
Here’s an easy code example:
// Our custom post type function
function
create_posttype() {
register_post_type( 'music',
// CPT Options
array(
'labels'
=> array(
'name'
=> __( 'Music'
),
'singular_name'
=> __( 'Music'
)
),
'public'
=> true,
'has_archive'
=> true,
'rewrite'
=> array('slug'
=> 'music'),
'show_in_rest'
=> true,
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype'
);
This code fragment registers the post-type music with an array of arguments. These are the options for our custom post type.
The array consists of two elements. The initial portion is labeled and is itself an array. The second section includes arguments such as public visibility, has archive, slug, and show in rest, which enable block editor support.
For a more detailed explanation, check WordPress’ official documentation.
How to display your custom post type on your WordPress website
WordPress includes support for displaying custom post types by default. Once you have added a few posts to your custom-made post type, it is time to show them on your website.
Navigate to Appearance > Menus to add a custom link to your menu. This is the link to your custom post type.
Your custom post type’s URL will most likely look like this if you are using SEO friendly permalinks:
If you are not using SEO-friendly permalinks, your custom post type URL will appear as follows:
https://example.com/?post type=music
Replace ‘example.com’ with your web domain and music with the name of your custom post type.
Visit the homepage of your website after saving the menu. When you click the newly added menu item, the archive page for your custom post type will be displayed using the archive.php template file from your theme.