Your Guide to WordPress Custom Post Types

Maddy Osman
Maddy Osman

Updated:

Published:

WordPress is the most popular content management system (CMS), and with good reason — the platform is user-friendly, straightforward, and powerful.

WordPress site owner creating custom post type

For many website builders, one of the biggest benefits of WordPress is the ability to jump right in, start editing and create a new post within minutes using the standard post types available in default WordPress deployments.

But what happens if you need a form or function that lies outside these typical WordPress post offerings? Armed with a bit of technical skill and the right knowledge, you can create custom post types that help your site stand out from the crowd. Here’s what you need to know.

Grow Your Business With HubSpot's Tools for WordPress Websites

What is a WordPress Custom Post Type?

The official WordPress website lists seven post types:

  • Posts
  • Pages
  • Attachments
  • Revisions
  • Navigation Menus
  • Custom CSS
  • Changesets

Posts are standard blogs and displayed in reverse sequential order, pages are more structured and often feature parent-child hierarchies, attachments hold information about any media uploads, revisions are used to create post histories, menus let users navigate your website, custom CSS can help modify the look of your site and changesets are designed specifically for the WordPress customizer.

In many cases, however, these seven post types aren’t enough to display and manage specific content on your WordPress sites. Custom post types offer a way to create and define your own post preferences, titles, and tags apart from WordPress standard formats and help your site get noticed.

How Do I Create a WordPress Custom Post Type?

There are two ways to create a custom post type: Using a WordPress plugin or digging into the WordPress PHP code to create your post type manually.

Using a WordPress plugin is by far the easier and faster choice since you’ll be able to choose post parameters from within your WordPress dashboard and quickly deploy the exact type of custom post you want. The caveat? These posts only last as long as your WordPress plugin is active. If you choose to delete the plugin or it’s no longer supported by new WordPress versions, your custom post type won’t work.

That leaves building your post type manually — but how do you get started?

Step-by-Step: Creating a Custom WordPress Custom Post Type

  1. Add code to your function.php file
  2. Check your site to ensure the new post type appears
  3. Customize the code with additional functions

Let’s explore each step in more detail.

1. Add code to your function.php file.

As noted by WPBeginner, there’s a standard code string that must be added to your function.php file to create the basis for your custom post type:

 

// Our custom post type function

function create_posttype() {

 

register_post_type( 'movies',

// CPT Options

array(

'labels' => array(

'name' => __( 'Movies' ),

'singular_name' => __( 'Movie' )

),

'public' => true,

'has_archive' => true,

'rewrite' => array('slug' => 'movies'),

'show_in_rest' => true,

 

)

);

}

// Hooking up our function to theme setup

add_action( 'init', 'create_posttype' );

 

 

2. Check that your custom post type appears in your WordPress Dashboard.

In the example above, the custom post type is called “Movies”. To ensure it’s working as intended, navigate to your WordPress dashboard.

If you’re successful, you’ll see a new category called “Movies” listed under “Comments”.

3. Customize your new post type.

Once you’ve confirmed the new post type is working, you can include additional functionality with this code:

 

/*

* Creating a function to create our CPT

*/

 

function custom_post_type() {

 

// Set UI labels for Custom Post Type

$labels = array(

'name' => _x( 'Movies', 'Post Type General Name', 'twentytwenty' ),

'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentytwenty' ),

'menu_name' => __( 'Movies', 'twentytwenty' ),

'parent_item_colon' => __( 'Parent Movie', 'twentytwenty' ),

'all_items' => __( 'All Movies', 'twentytwenty' ),

'view_item' => __( 'View Movie', 'twentytwenty' ),

'add_new_item' => __( 'Add New Movie', 'twentytwenty' ),

'add_new' => __( 'Add New', 'twentytwenty' ),

'edit_item' => __( 'Edit Movie', 'twentytwenty' ),

'update_item' => __( 'Update Movie', 'twentytwenty' ),

'search_items' => __( 'Search Movie', 'twentytwenty' ),

'not_found' => __( 'Not Found', 'twentytwenty' ),

'not_found_in_trash' => __( 'Not found in Trash', 'twentytwenty' ),

);

 

// Set other options for Custom Post Type

 

$args = array(

'label' => __( 'movies', 'twentytwenty' ),

'description' => __( 'Movie news and reviews', 'twentytwenty' ),

'labels' => $labels,

// Features this CPT supports in Post Editor

'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),

// You can associate this CPT with a taxonomy or custom taxonomy.

'taxonomies' => array( 'genres' ),

/* A hierarchical CPT is like Pages and can have

* Parent and child items. A non-hierarchical CPT

* is like Posts.

*/

'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( 'movies', $args );

 

}

 

/* Hook into the 'init' action so that the function

* Containing our post type registration is not

* unnecessarily executed.

*/

 

add_action( 'init', 'custom_post_type', 0 );

 

This allows you to add revisions, custom fields and parent/child category relationships to build out the functionality of your new custom post type.

While there are certain restrictions — the name for new post types cannot exceed 20 characters and can only include alphanumeric characters — WordPress allows substantial freedom when it comes to creating custom page types and adding new functionality.

The WP_Query Class for Custom Post Types

As your custom post type becomes more complex and holds an increasing amount of pages, images and other media it’s worth considering the WP_query class which allows you to create custom search conditions.

Let’s consider our “Movies” category again. Integrating this code will return any posts from the Movies category:

 

<?php

// The Query

$the_query = new WP_Query( 'category_name=movies' );

 

?>

To display the results of this query, developers must implement additional code:

 

<?php

// The Query

$the_query = new WP_Query( 'category_name=movies' );

 

// The Loop

if ( $the_query->have_posts() ) {

echo '<ul>';

while ( $the_query->have_posts() ) {

$the_query->the_post();

echo '<li>' . get_the_title() . '</li>';

}

echo '</ul>';

} else {

// no posts found

}

/* Restore original Post Data */

wp_reset_postdata();

 

?>

Using WP_Query, it’s possible for WordPress developers to create advanced queries which return specific results from custom pages on-demand.

WordPress Custom Post Type Plugins

As noted above, while it’s possible to create custom post types manually, it’s much quicker to create and deploy these custom types using a WordPress plugin. While these post types will stop working if you remove the plugin or it's no longer supported by the latest version of WordPress, plugins are a great way to get started with custom post types and discover the best fit for your WordPress website.

Some popular WordPress custom post type plugins include:

Custom Post Type UI

Custom Post Type UI offers a streamlined and simple interface to help create and manage custom post types on your WordPress site.

Post Types Unlimited

Post Types Unlimited works with any theme and lets you quickly create both custom post types and custom taxonomies.

Toolset

Toolset is a for-pay, all-in-one plugin that makes it easy to create, manage and configure custom post types.

WPK Custom Post Types and Custom Fields Creator

This plugin comes with three tools — a custom fields creator, post type creator and taxonomy creator — to help create, manage and maintain custom post types.

Flexibility in the Framework

WordPress offers powerful out-of-the-box performance but as your site evolves you may need form and function the basic WordPress deployment simply doesn’t offer. In this case, custom post types are often the easiest way to enhance website functionality within the existing WordPress framework.

Use HubSpot tools on your WordPress website and connect the two platforms  without dealing with code. Click here to learn more.

Related Articles

Capture, organize, and engage web visitors with free forms, live chat, CRM, analytics, and more.

DOWNLOAD THE FREE PLUGIN