Custom post types for WordPress are exactly what they sound like. They’re customized versions of “posts” and “pages”. Hand-picked names make them more useful for your website and users. They help you organize and segment different types of content.
And, a decoupled architecture makes them more flexible for your needs. Reasons to use WP custom post types are infinite.
In this article, learn how to add custom post types to your WordPress website.
- Add custom post types manually
- Using a custom post type generator
- Creating your own custom post types plugin
- Using a WP plugin boilerplate generator
- Using existing WordPress custom post type plugins
Benefits of WP Custom Post Types
WP custom post types have lots of benefits. These are just a few.
- Segmented content using custom post types
- Intuitive names for WP post and page content
- Improved user experiences using custom post types
- Increased content portability using custom post types
- Added confidence for website redesigns
Segmented Content
Segmenting content within WordPress gives you more power and flexibility to manage different types of content.
Imagine you want to add a Team page to your website. In doing so, you create a custom post type called Team and add each member of your team as a custom post. This way, you can add custom fields and formatting specific for team members without affecting blog post content.
Here, custom team members and blog posts are both posts. The difference is that team members are segmented and separated as a custom post type. You’re able to manipulate them independently, so you can control changes rather than working with global adjustments.
Intuitive Naming Conventions
Not everyone understands posts and pages — or the WordPress way. You can use posts and pages to create every sort of content your site may ever need, but jamming all your content under the same label and relying on fancy tagging and categorization techniques isn’t straight forward.
Extending the team example from above, you could create team members as posts and add them to a category called Team Members to segment them from normal blog posts. However, now you have to edit your theme and template files to exclude posts with the category Team Members from anywhere you’re displaying blog posts – and vice versa, exclude all posts not categorized as Team Members from displaying on your Team page.
Custom post types allow you to create your own naming conventions. You can think up names that work better for your website and appear intuitive for your users. With custom post types, when someone wants to add a team member they can easily go to the Team page in the dashboard instead of figuring out frail categorical hierarchy systems.
Improved User Experience
Custom post types simplify the content creation process. They make it easier for everyone from admins down to authors and contributors — not to mention, for the developers responsible for coding your website’s theme. CPT’s make it easier for everyone working on your website to do their job.
On the back-end, it’s easier to find, add, edit, and manage content.
On the front-end, it’s easier to theme and template.
Content Portability
Custom post types are stand alone pieces of content that can be embedded in posts and pages. Uncoupling sectional content from pages means the content can be displayed in more than one place. It means your content is portable and can be multi-purposed.
Sure, a custom post type can be a full-fledged page — just like a single post — but it can also be so much more.
Confidence to Redesign
Custom post types aren’t tied to your website’s design.
With WordPress, a redesign is as simple as changing themes. Using custom post types to create dynamic content that is decoupled from your website’s design gives you infinite flexibility to reskin or redesign your website whenever and however you want. You may need to recreate the pages that display your content when you swap themes, but you won’t lose any of your custom post type content when you do it.
How to Add Custom Post Types to WordPress
There’s three basic ways you can add a custom post type to your WordPress site:
- Manually add a custom post type
- Create your own custom post type plugin
- Use an existing custom post type plugin
Manually Add Custom Post Types to WP
You can manually add custom post types in your theme’s functions.php
file.
Adding a custom post type can be as simple as the code below.
add_action('init', function() { register_post_type('toaster', [ 'public' => true, 'label' => 'Toasters', 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ), ]); });
This would register a WordPress custom post type called Toasters in your theme. It’s a basic minimum-viable-function.
That’s the quick-start easy way to create a custom post type. However, WordPress is extremely flexible as a content management system. Behind the scenes, WP will fill in missing options with default values, but you also have the option to customize everything to one yourself.
Here’s the long-form approach to manually adding a WP custom post type:
/** * * Function to create a custom post type * */ function your_new_custom_post_type() { // Set up the custom post type's UI labels $labels = array( 'name' => _x( 'Toasters', 'Post Type General Name', 'twentytwentyone' ), 'singular_name' => _x( 'Toaster', 'Post Type Singular Name', 'twentytwentyone' ), 'menu_name' => __( 'Toaster', 'twentytwentyone' ), 'parent_item_colon' => __( 'Parent Toaster', 'twentytwentyone' ), 'all_items' => __( 'All Toasters', 'twentytwentyone' ), 'view_item' => __( 'View Toaster', 'twentytwentyone' ), 'add_new_item' => __( 'Add New Toaster', 'twentytwentyone' ), 'add_new' => __( 'Add New', 'twentytwentyone' ), 'edit_item' => __( 'Edit Toaster', 'twentytwentyone' ), 'update_item' => __( 'Update Toaster', 'twentytwentyone' ), 'search_items' => __( 'Search Toaster', 'twentytwentyone' ), 'not_found' => __( 'Not Found', 'twentytwentyone' ), 'not_found_in_trash' => __( 'Not found in Trash', 'twentytwentyone' ), ); // Set up additional options for Custom Post Type $args = array( 'label' => __( 'toasters', 'twentytwentyone' ), 'description' => __( 'Toaster products and reviews', 'twentytwentyone' ), 'labels' => $labels, // Add Feature Support (for the post editor) 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ), // Associate this custom post type with a taxonomy or custom taxonomy. 'taxonomies' => array( 'brands' ), /* Hierarchical custom post types act like Pages and can have * parent and child items. A non-hierarchical * custom post type acts like a Post. */ 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'menu_position' => 5, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'post', 'show_in_rest' => true, ); // Registering your Custom Post Type register_post_type( 'toasters', $args ); } /** * * Hook into the 'init' action so that the function containing * the custom post type registration is executed at the right time. */ add_action( 'init', 'your_new_custom_post_type', 0 );
As you can see, there are a lot of options available if you want to take the time to set them.
The full-code can also go in your theme’s functions.php
file.
Further Organization
You may find it easier to manage manually added custom post types if you put them each in their own special file and include that file in functions.php instead of pasting in the code directly.
Example:
- Create a new directory called
includes
. - Paste custom post type registration code into it’s own file called
cpt-toasters.php
- Save
cpt-toasters.php
in theincludes
folder (includes/cpt-toasters.php
) - Add the custom post type to
functions.php
usinginclude_once('includes/cpt-toasters.php');
/** * functions.php */ // ... other functions above // add the custom post type registration code include_once('includes/cpt-toasters.php'); // ... other functions below
Using a Custom Post Type Code Generator
Now that you know what it takes to register a custom post type, you may find it easier to use a code generator to create the custom post type registration code instead of typing it yourself. Check out a custom post type code generator at GenerateWP.com
GenerateWP.com uses a GUI/form to help you spin up the code required to register a custom post type in your functions.php
or plugin files.
Creating Your Own WP Custom Post Type Plugin
A slightly more robust option for registering custom post is adding the registration code to a plugin.
Using a plugin decouples your custom post types from the theme(s) you’re using. That way, if you decide to change themes your post types will still be available to WordPress after you switch. It makes custom post types theme-independent vs. adding them directly to the functions.php
file and tying them directly to a single theme.
For a detailed guide on creating your own custom plugin check out this WP beginner’s tutorial from WPExplorer titled Writing a Simple WordPress Plugin.
The short version involves creating a folder and php file to register your plugin that contains the custom post type registration function to register your custom post type. It’s very similar to putting the code inside of your functions.php
file except adding a custom post type as a plugin places the custom post type outside of your WP theme.
- Navigate to the
/wp-content/plugins
directory. - Create a new folder that matches your plugin’s slug (ex.
<i>my-cpt-plugin</i>
) - Create a PHP file inside the new folder with the same name (ex.
<i>/my-cpt-plugin/my-cpt-plugin.php</i>
) - Inside the file, add the code to register the plugin and then the code to register the custom post type
<php /* Plugin Name: My CPT Plugin Plugin URI: http://link to your plugin homepage Description: This plugin registers my custom post type. Version: 1.0 Author: Your Name Author URI: http://link to your website License: GPL2 etc License URI: https://link to your plugin license */ /** * * Function to register your custom post type */ add_action('init', function() { register_post_type('toaster', [ 'public' => true, 'label' => 'Toasters', 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ), ]); });
Now, you can navigate to your WordPress dashboard to activate the plugin — which will make your new custom post type show up in the dashboard’s menu, so you can use it.
This create a simple WordPress plugin. There are no fancy frills, but it will do exactly what you need it to do.
Creating Advanced Custom Post Type Plugins
For advanced plugin creation, try a more robust professional approach — use a boilerplate WP plugin generator tool like wppb.me.
Use an Existing Custom Post Type Plugin
You don’t have to be a developer, know much about WordPress, or get your hands dirty with PHP files like all the stuff mentioned above. Using a custom post type creation plugin — i.e. a plugin that was built to build custom post types – you can generate your own custom post types without ever touching code.
Custom post types are a long-standing integral feature of WordPress. They are one of the many things that makes WordPress such a great option for building websites. They’ve been around for a long time and they’re regular actors on the WordPress stage. WordPress developers have created many amazing plugins to streamline and automate the process of creating custom post types.
Think of it like this — someone took the manual custom post type code and packaged it into the WP custom post type generator. Then, someone took the generator and packaged it into a plugin. Now, you can install and activate a plugin and generate your own custom post types from inside of WordPress.
For the simplest way to create new custom post types for your WordPress website check out the Custom Post Type UI plugin.
Custom Post Type UI is available from the WordPress plugin repository, so you can install and activate it from your WP dashboard. As you can see, it gives you all the necessary settings (as well as the optional ones) in an intuitive easy to use graphical interface.
WordPress is for Everyone
WordPress is for everyone and custom post types make WordPress better. It doesn’t have to be hard to create a website.
0 Comments