How do I create a WordPress Custom Post Type (CPT) Without A Plugin?
By now, you already know that WordPress is a powerful Content Management System (CMS). But, did you know that WordPress is used by over 25.6% of all websites in the world? That’s an impressive number! Out of the box, WordPress comes with two main content types: Pages and Posts. These default content types may work for some websites, but a lot of websites today require more flexibility.
Introducing Custom Post Types
Custom Post Types are just like posts or pages; the difference between the default post types and CPTs centers around the ability to name a post type whatever you want, assign it custom fields, customize its category structure, and limit access to certain user roles. Like with most things with WordPress, there are multiple ways for us to create these Custom Post Types.
There’s A Plugin For That
One of the largest benefits when working with WordPress is just how easy it is to find a plugin to help with development and content management. There are a couple plugins in the WordPress plugin repository that make creating Custom Posts Types a breeze, one of which being the widely used Custom Post Type UI. If you are looking for a quick and clean way to add CPTs to your WordPress site in minutes, by all means, check out this plugin.
Manually Creating CPT For WordPress
The problem with using a plugin to create your CPTs is that if the plugin is ever deleted, your CPTs will disappear until the plugin is reactivated. Since all post content for WordPress lives in a database, the post content will not be gone forever if a plugin is deleted, but it will be unregistered and inaccessible. Another reason to forgo a CPT plugin is if you plan to use your theme or custom plugin on a different database and you want to keep the Custom Post Type registered, but do not need to copy CPT content. A common reason for this is when a theme or plugin is being sold with default CPTs already registered for the user.
Sites like generatewp.com make it easy to generate a snippet of code that you can copy and paste into your theme’s functions.php file to create a Custom Post Type. To get started generating your Custom Post Type, go here.
The generator will have you input all of the unique information about your CPT, such as the function name, labels, supports, permalinks, and much more. Once you have finalized your settings, click the “Update Code” button and GenerateWP will update the code below. Once this is done updating, copy and paste the code into your theme or plugin's functions.php file.
After you have copied your generated code into your theme’s functions.php file, your CPT is ready to use. Head into your WordPress Admin area and check it out for yourself!
That’s cool and all, but how do I these CPTs to show up?
If you set 'has_archive' => true you will have an archive of your new CPT that uses your theme's archive.php file. Or, if you would like to customize your CPT's archive template, you can duplicate your archive.php and change the name to archive-[yourcptname].php. You can view this archive by going to http://yoursitename.com/yourcptname. If you need to see more than just an archive, you can create a new WP_Query to loop through the post type that you created. For more information on WP_Query, click here.
This is just the beginning
This is just scratching the surface of what you can do with Custom Post Types in WordPress. How are some ways that you have used Custom Post Types in your WordPress themes or plugins?