Blog

WordPress Custom Post Types – Beginner’s Guide

WordPress isn’t just a tool for writing and publishing blog posts anymore. Over the years, it has evolved into an extensible, flexible and robust content management system. WordPress is slowly but surely moving towards becoming a fully-fledged framework for building web apps. One of the things that makes this CMS so adjustable, is Custom Post Types.

By default, WordPress comes with 5 content post types, of which, 2 should sound very familiar to you: post and page. But users can create as many custom post types as they need, and use them for tweaking WordPress to meet their needs. In this article, we will get familiar with WordPress custom post types, and how they can help you build awesome websites. We will cover two ways for creating custom post types: manually and by using a plugin.

WordPress Custom Post Types

Out of the box, WordPress comes with the following post types:

  • Post
  • Page
  • Revision
  • Attachment
  • Navigation Menu

WordPress treats each of its post types differently — by allowing unique options and features. A simple example, would be categories and tags, which are exclusive to the post type “post”. You can set your blog post to a specific category, but you can’t do the same for a static page. Pages on the other hand, can have parents and templates.

The post types mentioned so far are reserved by WordPress. Users can create their own custom post types to extend the website’s functionality, and better organize their content. Usually, creating custom post types goes hand in hand with creating custom fields and custom taxonomies, that allow you to insert unique content and options — specific to a given post type. But let’s leave these for another time, and focus on setting up a unique post type for our website.

Custom Post Type Examples

A post type can be any type of content that you need for your website. Let’s say that we have a podcast website that needs to display podcast episodes, and needs to publish occasional yet separate blog posts. Now, if we mix all of our content (Podcasts and blog posts) in one place, our content section will become really messy really fast. That’s why for our occasional blog posts, we will use the default post type “post” in WordPress, that allows us to use the default taxonomies: categories and tags.

As for our Podcast section, we will create a custom post type called Podcast. This way, we can separate the two different content types on the website, and have them organized in a neat way. Later, we can create custom fields for our podcasts section like episode duration, show hosts, links and so on. Additionally, we can setup custom taxonomies specific to this type of content, like Podcast categories, Podcast Shows and more. You see where this goes, and how much flexibility WordPress offers in terms of content manipulation and structure.

Creating a Custom Post Type in WordPress

There are 2 ways you can create a custom post type in WordPress: manually (fairly easy, but requires some familiarity with coding) and using a plugin (much easier method, but requires you to keep the plugin installed). Which one should you choose? Having many plugins can cause you headaches because of incompatibility, performance issues and bad code. If you already have tons of plugins installed, you may want to get your hands dirty this one time. We will cover both methods, and you decide which one works for you the best.

Using a Plugin

There are a handful of plugins for creating custom post types. One of the most popular ones, is Custom Post Type UI — which also allows you to create custom taxonomies for your post type.

Important note: Post types that are created with a plugin will disappear if you remove the plugin in question. The data will be in the database, but your custom post type will become unregistered, and not reachable from the admin area.

The interface of this plugin is pretty intuitive and simple. You click Add New, and then provide a post type name, label, singular label and description for the custom post type. Not that the 5 WordPress post types are reserved, and you can’t use those names. Here’s an example of what you should input:

Post Type Name: Podcasts
Label: Podcasts
Singular Name: Podcast
Description: Post type for podcasting

The singular name field is used by WordPress to display some UI elements and actions like Add New Podcast.

Manually

To create a custom post type manually, you need to modify your theme’s functions.php. Use your favorite FTP client and login to the server on which your website is hosted. Once connected, you need to:
1. Navigate to wp-content -> themes -> your theme
2. Open functions.php
3. If you are using a child theme, and there is no such file, create it.
4. Insert the code bellow.

As per our example, we need to create a podcast custom post type. Here’s the code for that (from step 4: the following code goes into your functions.php file):

// Registering CPT
function podcast_post_type() {

//Setting up the user interface labels for podcasting CPT
$labels = array(
'name' => _x( 'Podcasts', 'Post Type General Name' ),
'singular_name' => _x( 'Podcast', 'Post Type Singular Name' ),
'menu_name' => __( 'Podcasts' ),
'name_admin_bar' => __( 'Podcast' ),
'parent_item_colon' => __( 'Parent Podcast' ),
'all_items' => __( 'All Podcasts' ),
'add_new_item' => __( 'Post New Podcast' ),
'add_new' => __( 'New Podcast' ),
'new_item' => __( 'New Podcast' ),
'edit_item' => __( 'Edit Podcast' ),
'update_item' => __( 'Update Podcast' ),
'view_item' => __( 'View Podcast' ),
'search_items' => __( 'Search Podcasts' ),
'not_found' => __( 'No Podcasts found' ),
'not_found_in_trash' => __( 'No Podcasts found in trash' )
);

//Setting up other options for Podcasts CPT
$args = array(
'label' => __( 'Podcasts' ),
'labels' => $labels,
'description' => __( 'Creating and managing podcasts' ),
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments', 'revisions', 'custom-fields' ), //Initially supported features
'taxonomies' => array( ‘podcast-category'), //If custom taxonomies exist, insert their names in the array
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
);

//Registering custom post type
register_post_type( 'podcasts', $args );

}

// Hook into init action
add_action( 'init', 'podcasts_post_type', 0 );

This portion of code may look complicated but it really isn’t. You’re just inserting a bunch of options for the custom post type, and specifying what it will support. Note that:

  • The $labels array tells WordPress how to display the post type in the admin panel.
  • The $args array loads all the configuration from $labels array, and specifies the options for the custom post type.
  • The magic happens when the register_post_type( ‘podcasts’, $args ) function is called within the main podcasts_post_type() function with 2 arguments: the name of the custom post type and the options stored in $args (which is an associative array). Then, we just hook the podcasts_post_type() function to init, and let WordPress load it.

When you click save, and you upload the modified functions.php to your theme’s folder, you should see Podcasts (just bellow posts, because we set the menu_position in $args to 5) on your WordPress admin panel. If you hover it, you should see actions like “All Podcasts” and “Add New Podcast” — just as we specified in the $labels array. Since we didn’t insert any custom fields, when you try to add a new podcast, everything will be the same as if you were adding an ordinary post.

If you did notice, in the $args array, we added ‘podcasts’ as a taxonomy. We haven’t created this taxonomy yet, but this is ultimately going to hold our custom categories like Podcasts Categories, Podcast Shows, etc.

When it comes to WordPress custom post types, this is really all you need to now. Now the question becomes, how to display content from your CPT, how to create taxonomies (in our case, the podcast taxonomy), and how to create custom fields? Don’t worry, we are going to cover all of these, step by step!



Scroll to top