Pages and posts are the default post types provided by WordPress. But did you know you can add custom post types to your WordPress site?
Today we’ll talk about the custom post types we can include in WordPress. Since WordPress 3.0 came out we have the possibility to expand the range of available content. But when do we need to do this? Well, everything will depend on how you want to organize the contents of your website.
When to Create a Custom Post Type in WordPress?
Imagine you have a WordPress website and want to have a blog to tell your experiences. Well, here the Post concept allows you to get what you want. And if you also want to add several extra pages such as the contact page, the home page, etc., no problem: WordPress also allows you to create Pages by default.
Now you turn into a foodie lover and want to open a new section on your website with your grandmother’s recipes. If it’s going to be a temporary thing, you’ll simply create a page where you’ll put those recipes, or even a post on your blog. But if the thing goes further, you should create the content type Recipe so that each of your recipes has its place and behaves as a specific post within this content type. In addition, this will keep everything well organized and separated from the rest of the pages and blog posts.
How to Create a Custom Post Type in WordPress With PHP Code
To create the custom post type you want, you have several options. The most convenient is to create a plugin for your WordPress that includes the definition of these types of content. This way, whether you change the theme or not, the plugin will always remain on your website and the definition of custom post types will remain active.
To create a custom content type in WordPress we use the
register_post_type function. You can find its definition and examples of use in the WordPress Codex (the reference documentation for all WordPress programmers).
In the following fragment you have the minimum code to create a custom post type (in this case for Recipe). As you can see, it’s not too complex, is it?
If you put this code into your WordPress, you will get something like the first screenshot I showed you. When you include a new custom post type in WordPress, a new menu will appear on your dashboard with the post type. But you can customize this behavior and hide the menu (among many other things).
register_post_type function has a lot of parameters that you can use to register your custom post type one way or another. Some of the most important are the following:
show_ui: if you don’t want the custom post type to appear in the dashboard, use
falseas the value of this optional parameter. You also have the parameter
show_in_menuif you don’t want to have the new menu in the WordPress dashboard but still want the custom post type to be public.
exclude_from_search: if you don’t want this type of content to appear in the search results of your website, set this optional parameter to
true, the post type behaves live a page, allowing to have a hierarchy of contents and subcontents. When
false, there is no hierarchy, as with the posts of a blog.
true, the content type will be accessible from the WordPress REST API, and you can also edit it using Gutenberg. In case you want the opposite, put a
supports: allows you to define whether the content type supports title, content, authors, featured image, etc. This way you can define which elements will be loaded when you go to edit the specific content. If you allow support for authors and featured images, when editing you will see the metaboxes to select an author and a featured image. You have all the information here.
labels: includes all the labels used in the WordPress interface for the post type. This way you can translate them.
If you want to be able to modify all these parameters without missing anything and in a simple way, the best thing to do is to visit GenerateWP, a website that allows you to easily generate the code to create the custom post type in WordPress. Just follow the steps and the website will generate the code you need.
Once you’re done, copy and paste the code into your WordPress plugin and you’re done. Now you have your custom post type up and running!
How to Create Custom Post Types in WordPress With a Plugin
Creating a custom post type in WordPress if you don’t have any programming skills might seem complex, but luckily for you there are plugins that make it easy. Although there are several, the one I have always used and totally recommend is Custom Post Type UI.
Just install and activate it in your WordPress to start creating your own custom post types without having to write any code anywhere. You will see a new menu on the WordPress Dashboard from where you can create the custom post type.
In addition, its features include the ability to export and import the definition of custom post types, which is great if you want to move a custom post type from one site to another. It even allows you to get the PHP code in case you want to stop using the plugin and use the code directly as we saw in the previous section of this article!
Custom post types expand the possibilities that we can achieve with WordPress. If you didn’t know them, I encourage you to try them out and think about the content structure of your website. It’s quite possible that custom post types might help you to organize everything in a much more intelligent and interesting way.
Beware of custom post types that some themes include. If you use those post types and then change the theme and the new theme no longer defines the same post types, they will simply disappear from your WordPress website. In fact, they won’t be seen on the front of the page for your visitors or on the dashboard for your users (editors, authors, etc)—it’ll be like they never existed! That’s why you should have the definition of your post types in your own plugin for your website, as I told you earlier.