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 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 them both manually and using a 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.
WordPress comes pre-stocked with several post types that youāll likely be familiar with, including:
- Post: The traditional āblogā 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 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 in WordPress is with a 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 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 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 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 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 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 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 your regular blog posts. 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 content 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 hosting solutions that are tailored to meet your WordPress needs. Check out our innovative resources and hosting plans today!