What are WordPress Custom Post Types?
Custom post types are an excellent method to create and organize various types of content on your WordPress site.
Custom Post Types allow you to create a more intuitive back-end for people working on your site. They also allow you to assign custom front-end designs to different types of content for your site’s visitors.
To understand why we use custom post types, we need to understand the evolution of WordPress.
Evolution, not Revolution
WordPress began life as a blogging platform. Its sole purpose was to allow users to share articles on their blog. A simple WordPress site just had posts with a page dedicated to showcasing those posts.
In WordPress version 1.5, released in February 2015, users gained the ability to manage static pages. Pages are actually just custom post types: the very first custom post type.
By version 2.9, you could create your own custom post type. WordPress is now a fully featured Content Management System. However, there was never any great revolution in the way WordPress managed content. They just gave you the ability to customize your posts.
When to Use Custom Post Types?
You should use custom post types when you want to create content that will be treated differently to your pages and posts. This could mean you want it organized differently in the backend of the site. You may want to have stylistic differences between this content and other content. You might just want it to appear in a different place.
For example, say you are an artist who creates cat memes. On your WordPress site, you have your posts where you talk about where you get your inspiration from. You have About and Contact Us pages. The final pieces of content are the cat memes themselves. Does this call for a custom post type?
On your posts page your content outputs with an author field and a date. You don’t want these on your static pages – when have you ever seen the publish date on an About page? You want to have the cat’s name appear above your cat memes, but don’t want text above your photos in your pages or posts.
You also want to have a separate page for all your cat memes to appear; you don’t want them to appear in the regular blog stream.
This is definitely a case where creating a custom post type called Cat Memes would be appropriate.
How to Create Custom Post Types?
Like most things in WordPress, there’s a plugin for that! You can also write your own custom post types using PHP code, or use an online tool to output the code for you.
Creating with Plugins
The best-known plugin to register custom post types is a plugin called Custom Posts Types UI.
After installing and activating the plugin, you will be able to access it from the left-hand menu bar by clicking on the ‘CPT UI’ menu item.
This will take you to the Add/Edit post types screen where you have three required fields to fill in: Post Type Slug, Plural Label and Singular Label. You can use whatever names you like for these fields, though the post type slug has conditions explained beneath the text entry field.
Beneath these required fields are labels and settings. The labels affect what you see in the WordPress backend. The default settings will suffice for most use cases. You can come back to review these later by using the Edit Post Types tab once your new custom post type has been created.
Your new custom post type should show up in the left-hand sidebar of the WordPress dashboard beneath posts and pages. You can edit how the post type looks by creating a new theme file or using a page builder to create a new layout once inside the post editor.
Creating a New Custom Post Type with Coding
There is a debate within the WordPress community about where your custom post type code should live. While it will work perfectly well within functions.php, there are disadvantages. Should you be building a site for a client, they will lose access to the custom post type if they were to switch themes in the future. Hence, most would argue that custom post types should be registered within a plugin. If that doesn’t concern you, feel free to just add it into functions.php.
A simple version of the custom post type function can be seen below:
The WordPress command that allows the registration of a custom post type is:
register_post_type( $post_type, $args );
The $post_type variable matches to cat_memes in the above screenshot.
The $args variable has been replaced by an array in the above screenshot. The likelihood is that you will want to use a larger array. It may be more readable to leave the $args variable in place and set the $args variable equal to the much larger array. All the arguments that you can pass to this $args array can be found on the register_post_type page on the WordPress codex.
You also have to include your register_post_type function within another function, and call it at the ‘init‘ hook, in order for it to work.
If you are comfortable with WordPress files but not too keen on writing your own custom post type function, you can use the Post Type Generator on generatewp.com.
This gives you an interface in which to enter each attribute of your custom post type. The generator will then output the code required for you to create a custom post type. Simply copy and paste the code into your plugin or functions.php file.
If you are comfortable with creating custom post types, presumably you want to control how they are output on the front-end. To create a new template for your custom post type, create a new file within your theme. You should name this file ‘single-[post_type]’, replacing [post_type] with the name of your custom post type. In our example, this would be ‘single-cat_memes’. If this doesn’t exist, it will default to the single.php file used by regular posts on your site.
if you are using an archive page to display your custom post types the same logic applies, with our example ending in ‘archive-cat_memes’.
And that’s all there is to it. Your WordPress site is now better organized and ready for your cat memes.
Have questions? Feel free to post your queries below or visit our contact us page to get in touch.
Did You Like Our Post? Sign Up Below and Get Latest Content Directly Delivered to Your Inbox
Enter Your email ID and be the first one to know when the new post gets published on SurfnLearn.