This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Try the fastest hosting platform with pay-as-you-go pricing & 24/7 expert support! MIGRATE NOW →

How to Create Custom Post Types in WordPress (via Plugin and Code)

Updated on November 22, 2023

9 Min Read
wordpress custom post type

When you install WordPress, you’re initially provided with three built-in content types: posts, pages, and media. However, as the diversity of usage increases, these standard options may not meet requirements for everyone’s needs.

To address this issue, WordPress has evolved to become more flexible and advanced. That’s where WordPress custom post types come into play. They offer a way to create and manage content that goes beyond the standard posts, pages, and media.

In this blog post, I’ll guide you through creating and managing custom post types in WordPress, from understanding their importance to troubleshooting common issues. Let’s get started to take your content creation to the next level!

What Are Custom Post Types?

Custom post types in WordPress can turn your website into a powerful CMS. They allow you to create different types of content beyond the standard posts, pages, and media. There’s no limit to the number of custom post types you can create.

For instance, if you run a News website, you can create a custom post type titled “News”. This new type will have its own dedicated section in the WordPress dashboard admin area. You can create as many post types as needed, such as “Movies”, “Portfolios”, and more.

If you’re using WordPress 5.x, a detailed guide is available on enabling the Gutenberg Editor for your custom post type. For more information on other post types in WordPress, refer to the Custom Post Types codex documentation.

Looking to smoothly implement custom post types on your site?

Experience effortless WordPress customization with Cloudways WordPress Hosting.

Default vs Custom Post Types

Both default and custom post types have their own uses and can coexist on your WordPress site to provide a rich and diverse content experience. Look look at their differences!

Default Post Types Custom Post Types
Availability Available by default when you install WordPress. Need to be created manually or with a plugin.
Types Posts, Pages, and Media. Unlimited, based on your needs (e.g., News, Movies, Portfolios).
Flexibility Limited to the built-in options. Highly flexible, can be customized to fit your specific needs.
Usage Ideal for standard blog posts, static pages, and media files. Ideal for specialized content that doesn’t fit into the default post types.

Why Use Custom Post Types?

Here are a few more reasons why you should use custom post types in WordPress:

  1. They help keep your website tidy by separating different kinds of content.
  2. They make it easier for visitors to find what they’re looking for on your site.
  3. They let you add special features to certain posts, like review scores for book reviews.
  4. They can help your site show up higher in search engine results.
  5. They make it easier to create consistent, structured content, especially when multiple people are adding content to your site.

Create Custom Post Types in WordPress

Creating custom post types in WordPress can be done using two different methods: via plugin or manually. Each method caters to different user skills and preferences. Let’s look at them step by step.

Method 1: Create a Custom Post via Plugin

You can create a WordPress custom post type easily using the right plugin. For this article, I’ll use the Custom Post Type UI plugin to guide you through the process.

Step 1: Install and Activate the Plugin

The first step is to install and activate the plugin.

  1. Go to your WordPress Dashboard;
  2. Select Plugins Add New;
  3. Search for the ‘Custom Post Type UI’ plugin;
  4. Install and Activate the plugin.

Custom Post Type UI’

Step 2: Set up and Configure the Plugin

Once activated, you’ll find a new menu item in the dashboard called CPT UI.

  • Click on CPT UI → Add/Edit Post Types to create a new custom post type;
  • Fill in the Post Type Slug, which is a required field and must be unique (usually a lowercase string with no spaces);
  • Add the plural and singular names for your custom post type as they will appear in the dashboard;
  • Click on the Add post type button.

Add post type

Step 3: Use Your Custom Post Type

Your new custom post type should now be visible in the WordPress Dashboard. You can start adding new content by clicking on the menu item for your custom post type.

menu item

  • Go to Add New and customize your custom post type.

Go to Add New

  • I will add a quote and contact form on my news page;
  • To do that click on the + (plus) sign;
  • Select Pull Quote.

Select Pull Quote.

  • Select Contact Form 7.

Select Contact Form 7.

  • Click on the Publish button.
  • Here’s what the final news page looks like.

final news page

Method 2: Create WordPress Custom Post Manually

Now, let’s follow these steps to create a custom post type manually on your WordPress site:

  1. Navigate to the function.php file on your theme directory;
  2. Add the following code to the function.php file.
/* Custom Post Type Start */

function create_posttype() {

register_post_type( 'news',

// CPT Options

array(

'labels' => array(

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

'singular_name' => __( 'News' )

),

'public' => true,

'has_archive' => false,

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

)

);

}

// Hooking up our function to theme setup

add_action( 'init', 'create_posttype' );

/* Custom Post Type End */
  • Once you’ve added the code, the News post type will automatically appear in your WordPress menu.

 News post type

  • When creating custom post types, it is necessary to use init for the hook in add_action(), and the register_post_type() function will take the arguments
/*Custom Post type start*/

function cw_post_type_news() {

$supports = array(

'title', // post title

'editor', // post content

'author', // post author

'thumbnail', // featured images

'excerpt', // post excerpt

'custom-fields', // custom fields

'comments', // post comments

'revisions', // post revisions

'post-formats', // post formats

);

$labels = array(

'name' => _x('news', 'plural'),

'singular_name' => _x('news', 'singular'),

'menu_name' => _x('news', 'admin menu'),

'name_admin_bar' => _x('news', 'admin bar'),

'add_new' => _x('Add New', 'add new'),

'add_new_item' => __('Add New news'),

'new_item' => __('New news'),

'edit_item' => __('Edit news'),

'view_item' => __('View news'),

'all_items' => __('All news'),

'search_items' => __('Search news'),

'not_found' => __('No news found.'),

);

$args = array(

'supports' => $supports,

'labels' => $labels,

'public' => true,

'query_var' => true,

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

'has_archive' => true,

'hierarchical' => false,

);

register_post_type('news', $args);

}

add_action('init', 'cw_post_type_news');

/*Custom Post type end*/

Here’s the code explanation:

  • $supports: Specifies that the post type is compatible and supports all essential features.
  • $labels: Specifies that the post type is referred correctly to the admin area.
  • $args: Specifies a permalink slug of the news and a menu position located just beneath the Posts menu.

Here’s the look at before and after adding custom post features to your WordPress site.

Before adding custom post features on WordPress

– Before adding features

After adding custom post features on WordPress

– After adding features

The steps we’ve covered so far have shown you how to register custom post types to the backend of any WordPress theme. Now, let’s move forward and learn how to add a custom post to your WordPress site.

Add a New WordPress Custom Post Type

You can add a new custom post on your WordPress site by following these steps:

  • Click registered custom post type, which in our case is News;
  • Click Add New;
  • Type the title and body of your post;
  • Type the excerpt and set a featured image;
  • Click on the Publish button to take the new custom post live.

Create a New Custom Post on a WordPress Website

Create a Template and Fetching List

Next, it’s time to create a template and fetch a list. For this, you’ll need to create a new file named template-news.php.

  • Place the newly created file in your theme folder;
  • Add the following code to the file.
<?php

/*Template Name: News*/

get_header();

query_posts(array(

'post_type' => 'news'

)); ?>

<?php

while (have_posts()) : the_post(); ?>

<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>

<p><?php the_excerpt(); ?></p>

<?php endwhile;

get_footer();

?>

Select a Template

  • Go to your WordPress Dashboard;
  • Click PagesAdd New;
  • Create a new page named News;
  • Click Page Attributes on the right side and access the drop-down menu under Template;
  • Select the new template News;
  • Click on the Update button to set your template.

Refer to the image below for the visual representation of the above steps:

Select template

The image below represents the final display of your listing page:

Listing Page Result

Add Menu for Custom Post Type

Add your custom post type as a part of the Menu options on your WordPress site by following these steps:

  • Go to your WordPress Dashboard;
  • Navigate to AppearanceMenus;
  • Add the News page to your main menu to display a navigational link to our newly created WordPress custom post type, News;

For further reference, check out the image below.

Add Menu for Custom Post Type

And this is how your website will look on the front end. Check out the image below:

Front end page result

Accelerate Your Website With Cloudways Managed WordPress Hosting

Create custom post types with complete freedom.

Create a Detail Page for Custom Post Type

Create a detail page for custom post type by following the steps below.

  • Add a new file called single-news.php (located in your WordPress theme);
  • Add the following code to the file.
<?php

get_header();

/* Start the Loop */

while (have_posts()) : the_post();

get_template_part('template-parts/post/content', get_post_format());

endwhile; // End of the loop.

get_footer();

?>

Now it’s time to see what your detail page looks like:

Detail Page for Custom Post Type

Customize Your Custom Post Types

Customizing your custom post types using the CPT UI plugin is way simpler. Here’s how you can do it:

  • Go to CPT UIAdd/Edit Post Types;
  • You will see all the settings. Play with those as per your needs;
  • For example, if you want to change the labels or add a new item, go to the additional labels and set them up.

Customize Your Custom Post Types

  • Similarly, play with other settings to customize it as you please.

Display Custom Post Type on your site

You can display custom post types on your site using the default archive template.

  • Go to Appearance → Menu;
  • Under the custom link, add your URL and Link Text.

Display Custom Post Type

  • Save the menu.
  • Reload the page, and you will see news on the top of your site’s front end.

Display Custom Post Type

Troubleshooting Common Issues

When working with Custom Post Types (CPTs) in WordPress, you might encounter a few common issues.  Here are the two most common issues that might show up and the ways to solve them.

1. Custom Post Type Not Showing up

Sometimes, you have added the custom post type, but it doesn’t appear. The possible reasons for this can be:

  • Incorrect code.
  • Plugin conflict.

Solution

  • You need to review the code to ensure that the code is right. Take expert help to do that.
  • Try deactivating the plugins to check that none conflicts with the custom post-type plugin you’re using.

2. Problems With Permalinks

If you’re experiencing issues with permalinks related to your CPT, such as 404 errors or incorrect routing, here’s how you can fix it.

Solution

  • Go to Settings → Permalinks;
  •  Click on the Save Changes button.

Problems With Permalinks

Also, sometimes a CPT might have the same slug as an existing page, causing conflicts. Ensure that your CPT slug is unique.

Summary

You have now learned how to create a custom post type in WordPress, which is quite complex but can be easily done by following the steps mentioned in this blog. You can use custom post types with our WordPress hosting.

Check out our other blog posts for more helpful guides on managing your WordPress site, such as reducing WordPress load times. If you have any queries or suggestions regarding the blog, feel free to comment below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Liza Rajput

Liza Rajput is a Technical Content Producer at Cloudways. Being a software engineer, she loves to play with data and its processes and wishes to grow and excel in Data Science and Big Data Engineering. She has also been an avid reader and exceptional writer, with sufficient experience in technical, research-based, and creative writing.

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now