WordPress Custom Post Types
Niche websites can sometimes have content organization needs that are not met by WordPress’ native post types. For that reason, you may have wondered if there are alternative options when it comes to organizing and displaying your content.
Fortunately, Custom Post Types (CPTs) are an option in WordPress that enables you to create a new kind of content and customize it however you like. This can be very helpful if you run a WordPress website that features content elements beyond the traditional posts and pages.
In this article, we’ll explain custom post types and their structures. Additionally, we’ll give you a walkthrough of how to create a WordPress custom post type both manually and using a WordPress plugin. If you’re ready, let’s get started!
What Are Custom Post Types?
Your WordPress database automatically creates a wp_posts table when you install the software on your web server. This table includes a post_type column, where the information we’ll be talking about is stored.
There’s several WordPress post types that you’ll likely be familiar with, including:
- Post: The traditional ‘blog’ WordPress post that appears in reverse chronological order, and can be assigned categories and tags.
- Page: These can be structured with parent-child relationships to create a hierarchy, but typically are not assigned categories.
- Nav Menu: A list of links that helps visitors navigate your website.
- Revision: These keep a log or history of changes to your other post types, in case you need to roll back to a previous version.
- Attachment: Any media you add to your posts or pages also gets stored as a WordPress post type, and your image or video file data is stored in the wp_postmeta table.
Additionally, WordPress includes two main taxonomy structures. Categories and tags are the primary way of assigning posts to different groups. While you can assign categories and tags to a CPT, you might find it necessary to also create custom taxonomies, in order to better organize your content.
One example of how this might apply is with products in an eCommerce store. You can create a ‘Products’ CPT with custom fields for information like pricing, quantity, and so on. Then, with a custom taxonomy you can create options for categorizing your products in just about any way you might imagine.
How to Create a Custom Post Type – Plugin Method (In 4 Steps)
The easiest way to create CPTs is with a WordPress plugin. Let’s walk through how that process works.
Step 1: Download a Dedicated Plugin
For this example, we’ll use the Pods – Custom Content Types and Fields plugin.
This tool enables you to easily create and customize your own post types and custom taxonomies. Once you install the plugin through your WordPress Plugins > Add New menu, you’ll want to activate it as well.
Step 2: Add a New Custom Post Type
Next, you’ll find a new Pods Admin option in your admin menu. From there you can select Add New, and choose between creating a new content type or extending an existing one.
For this example, we’ll select Create New to demonstrate the configuration options that are available.
Step 3: Configure Your New Post Type
On the Configure page, you’ll fill out your new content type’s options.
Here you can enter singular and plural labels for your new content type, as well as select what kind of custom content you are creating. This can be a post type, a taxonomy, or a settings page.
Step 4: Create Custom Fields
Once you’ve created your labels, you’ll be taken to another settings page where you can add new fields, among other things.
As you can see above, in our WordPress custom post type for products we’ve added a custom field for Price.
Once you’ve completed these settings, you can select Save Pod. You’ll now be able to find your Product custom post type in your main WordPress menu. From there you can navigate to Product > Add New, and begin creating content using this WordPress post type.
You’ll also see a Pods Shortcode button option in your post editor. This enables you to add field information from a variety of Pods items. Any custom fields you created earlier will also appear below your post editing window for easy access.
Once you edit your content, you can save and publish your custom post type as you would normally do in WordPress.
How to Create a Custom Post Type – Manual Method (In 3 Steps)
You can also create a CPT manually by editing your website’s functions.php file. We recommend making a copy of your file or backing up your site before starting.
Step 1: Locate and Open Your functions.php File
You can either access your site’s files through your web host’s file manager in your cPanel, or with a File Transfer Protocol (FTP) application like FileZilla. Once you’re connected you’ll navigate to your WordPress root folder, and then to wp-content > themes > your-theme.
It’s important to note that each theme has its own functions.php file. If you choose to use the manual process for creating CPTs, you’ll lose them if you change your WordPress theme. If you want to make sure your custom types are preserved no matter what, it’s best to use the plugin method instead.
Step 2: Insert a Custom Post Type Code
The amount of detail you can include in a CPT is pretty vast. We’ll use a simple example to demonstrate how you can create a ‘Product’ CPT. You’ll see the same kinds of label options and settings that were available in the WordPress plugin settings earlier:
//* Create Custom Post Type add_action( 'init', 'add_custom_post_type'); function add_custom_post_type() { register_post_type( 'my_products', array( 'labels' => array( 'name' => ‘Products’, 'singular_name' => ‘Product’, 'add_new' => ‘Add New Product’, 'add_new_item' => ‘Add New Item’, 'edit_item' => 'Edit Product', 'new_item' => ‘New Product’, ), 'public' => true, ‘has_archive => true, ‘rewrite’ => array( ‘Slug’ => ‘products’ ), 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes' ), ) ); }
You’ll want to add this code to the end of the functions.php file in your site’s theme folder. Of course, there are many more options that you can add in your CPT code as well, which can be found on resource sites like GitHub.
Step 3: Add Your Site’s Text Domain
If your site is translation ready and you want your CPT to be as well, you’ll need to locate your site’s text domain and make sure it’s included in the code you use for your CPT.
You can find your site’s text domain in the style.css file for your WordPress theme.
Once you open style.css, you will find “text domain” information in the file’s header.
You can then reference the text domain in your customizations. For example, in the “labels” array from the example code above, you would add your text domain after “Products” :
'name' => ‘Products’, ‘twentynineteen’,
You would add the text domain to all labels in your custom post type, in order to make it translatable.
Displaying Custom Post Types on Your Site (2 Methods)
Once you create your CPT, you have a couple of options when it comes to displaying them on your site. We’re going to cover two methods you can use.
1. Use a Default Archive Template
One way to make sure your CPTs will appear on your site is by adjusting the code you use to create them. You can include the following string in your array:
‘has_archive’ => true,
Once you do this, your CPT will have its own archive page based on your theme’s archive page template. You can then access your new CPT archive page by using the URL www.yoursite.com/customposttype.
2. Display Them on Your Front Page
One of the benefits of creating CPTs is the ability to keep certain content separate from a regular blog post. If you want to make sure this content makes it onto your front page, however, you’ll need to add a small snippet of code to your theme’s functions.php file:
// Display Custom Post Types on home page, add to functions.php add_filter( 'pre_get_posts', 'my_get_posts' ); function my_get_posts( $query ) { if ( is_home() && $query->is_main_query() ) $query->set( 'post_type', array( 'products' ) ); //add cpt, in this case 'products' to array return $query; }
Naturally, you would also customize this snippet to include your CPT’s name and other key details.
Customize Your Site With WP Engine
Custom post types are a dynamic way for you to further customize your WordPress site, and deliver well-organized custom content types to your site’s visitors. Here at WP Engine, we offer the best resources for users and developers, and can help you create an incredible digital experience for your customers.
In order to develop a truly engaging digital experience, you need managed WordPress hosting solutions that are tailored to meet your WordPress needs. Check out our innovative resources and hosting plans today!