What is a WordPress custom post type?
What are custom post types used for?
Custom post types are useful for adding items to a WordPress site that have the same purpose. They can resemble either pages or posts, but should not be confused with either of the default WordPress content types. Custom post types can be queried and displayed just like pages and posts. In addition, they can have an archive page just like posts.
Some examples of common uses for custom post types include podcasts, testimonials, and events. Podcast items typically resemble blog posts and have their own archive. It is useful to separate podcast items from blog items to make management easier. Similarly, testimonials and events can be more easily managed if they are separated out from pages and posts.
Custom post types have several built-in options that can be set when creating the custom post type. To extend the functionality you can add custom fields via code or the Advanced Custom Fields (ACF) plugin.
How do you create a custom post type?
There are several ways to create a custom post type depending on your level of development knowledge.
Experienced developers will opt to create a custom post type from scratch. The code for the custom post type can be added to the theme, but it is typically recommended to create custom post types as a plugin. (Creating a plugin instead of adding to a theme will allow the custom post types to remain usable if the theme is changed.)
My preferred way of creating custom post types is using a code generator provided by GenerateWP. The difference between this method and the PHP method described above, is that GenerateWP provides a friendly user interface for creating the custom post type function. Once you have generated the function, you can copy and paste the code to your functions.php file in your theme or create a custom plugin (the recommended approach).
If you have limited WordPress development experience you will probably be most comfortable using a plugin to add a custom post type. Some of the most popular custom post type plugins include Custom Post Type UI, Pods, and Toolset Types. These plugins offer easy to use interfaces for creating custom post types.
In addition to the plugins used to create truly custom post types, many plugins will create specific custom post types based on their intended use. For example, the Modern Events Calendar plugin will create a custom post type called “Event” with its own taxonomy, settings, and fields.
What are some common settings for a custom post type?
Regardless of the method you use for creating a custom post type, there are some common settings you will want to keep in mind.
Label settings determine how your custom post type shows up in the admin dashboard. You can choose to customize the labels as much or as little as you’d like. Typically the default label for a custom post type uses “item” or “items.” I find it most user-friendly to customize the labels for the end user.
Supports parameters specify which default WordPress features should be available in the custom post type. Some examples of features which can be optionally supported include custom fields, featured image, and comments.
Out of the box, WordPress posts come with the ability to use categories and tags. Custom post types can optionally use these taxonomies as well. In addition, you can create custom taxonomies to go with your custom post type.
The hierarchical option determines whether your custom post type behaves like a post or page. If the hierarchical option is set to true, your custom post type will resemble the page setup and its items may have parent/child relationships. Setting the hierarchical option to false will remove that ability.
Custom post types can optionally have a frontend archive. The archive for posts is typically the blog page, a page where all of the items of a content type are aggregated and displayed. Setting has_archive option to true will create an archive page on the frontend. The archive page can be edited through theme templates and/or CSS.
Show in REST
The show_in_rest option has become more important with the release of the newest WordPress editor (Gutenberg). If you have used WordPress for a long time you remember the old text editor. The newest drag-and-drop interface requires the REST API. Therefore, if you want your custom post type to use the drag-and-drop editor you will need to set show_in_rest to true. Otherwise you can set it as false.
You may wonder why you would ever not want to take advantage of the new editor. One reason would be if you want your custom post type to be restricted to a certain layout based on a set of custom fields. It may be more beneficial to minimize the default editor features in favor of adding your own custom fields.
There are more custom post type options than I am describing here, but the ones I’ve listed are the most important and most commonly customized. I recommend clicking through the Generate WP post type generator to get more familiar with all of the settings.
Custom post types are used to make a WordPress website easier to manage from the backend and easier to create theme layouts based on a content type. They can be added to a theme or as a plugin using PHP or via a custom post type plugin with a user interface. There are many ways to customize a custom post type to get exactly the functionality you need.