WordPress can display a plethora of different types of content, but organizing it can be tough. The default options are fairly limited, and customizing them can be confusing. That’s precisely why I decided to put together this brief guide.
By using custom post types, you can create a new type of item – like posts and pages – which will contain a different set of data. It’ll have its own admin menu, its own editing pages, its own custom taxonomies, and a bunch of other utilities.
If you’re wondering why you need one of these in the first place, they’re best for websites with content that is organized along an unusual structure. So if you have any content that you need to display differently than on regular posts and pages, a custom post type may be just what you need. They’re also great for SEO, due to their built-in permalinks.
What is a Custom Post Type?
A post type, despite the specific-sounding name, can be used for any kind of content. You’ve probably seen them before, since developers use custom post types to add portfolios, staff, testimonials and more to their WordPress themes. So a custom post type is just a regular post with a different post_type value in the database. There are five default post types: post, page, attachment, revision, and navigation menu. WordPress 3.0+, however, gives you the capability to add your own custom ones.
The term taxonomy comes up often in reference to custom post types and that might be a bit confusing to some. For those new to WordPress, taxonomies are a way to group posts and custom post types together. WordPress comes with four built-in ones: category, tag, link category, and post formats. You can learn more about the specifics of these over at the WordPress Codex. However, you can also create your own custom taxonomies and use them in your post types to group and sort content.
How to Create a Custom Post Type?
Adding custom post types in WordPress is extremely easy since WordPress includes the core function register_post_type that can be used to create them. This means if you are a plugin developer you can easily include custom post types in the theme you are creating. Or you can add them via your child theme or via a custom plugin.
Creating A Custom Post Type Manually (using code)
First things first, where should you add your code? The best place to register and add your custom post types depends on your project. If you are working on a client site that already has a theme active you’ll want to create a child theme and register your post types from there. If you are creating your own custom theme you can place the code in the functions.php file or in any other file called from your functions.php. And if you are developing a plugin it doesn’t really matter where you add the code, so long as the code runs before the ‘init’ action hook to make sure it’s available.
For testing purposes, your functions.php file will do just fine. But a plugin will ensure you won’t break
This article was written by Kyla and originally published on WPExplorer.