Chat with us, powered by LiveChat

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.

How to Create WordPress Custom Post Types

March 5, 2019

5 Min Read
create custom post types in wordpress
Reading Time: 5 minutes

When we install WordPress, we only get three built-in content types at the backend, i.e. posts, pages, and media. However, today WordPress has become quite flexible and advanced. Therefore, the approach to adding more post types have also diversified. The diversified usage demands for more content types because posts, pages, and media are not enough and here is where WordPress custom post type come in handy.

What is a WordPress Custom Post Type?

According to WordPress Codex, “Custom Post Types” also known as “Custom Content Types” is the specific type of post types that can be added to your WordPress using a simple function called the register_post_type(). The function allows you to add the new custom post type in accordance with a number of specifics such as supported features, availability, and labels.

Update: WordPress 5.x comes with brand new Gutenberg editor. If you wish to use Gutenberg with WordPress custom post types, here is a great guide on enabling Gutenberg editor for CPT.
Update: WordPress 5.x comes with brand new Gutenberg editor. Here is a detailed guide on enabling Gutenberg editor for custom post type

Other than that, one can find several post type that is available by default in WordPress installation.

  • Post – blog post
  • Page – static page
  • Attachment – attached media
  • Revision – post revision
  • Navigation Menu – nav menu

For further details on other post types in WordPress, please have look at custom post type codex documentation.

When it comes to custom post types, WordPress supports an unlimited number of Custom Post Types. You can create your own custom posts and can call them up, wherever you want. For example, if you run a News website and you wish to add a custom post type titled, “News”. Once created, the news post-type would have its own menu in the WordPress dashboard admin area. You can also create multiple post type as well such as Movies, Portfolio, and much more.

Create a WordPress Custom Post Type

To create a custom post type for any particular theme on WordPress, navigate to function.php file from your WordPress theme directory then add the following code to it.

/* 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 */

After adding this code, the News post-type will automatically appear in the Admin Area of your WordPress. To see how it will appear at the front-end of your WordPress dashboard, refer to the image below.

Creating a Custom Post Type

When you create custom post types, it is necessary to use init for the hook in add_action(). The register_post_type() function takes 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*/

$supports: Specifies the post type is compatible and supports all essential features.

$labels: Specifies that the post type is referred properly to the admin area.

$args: Specifies a permalink slug of the news, and a menu position located just beneath the Posts menu.

Now let’s take a look before and after adding features to our WordPress website.

Add New Post
Before adding features
After adding features
After adding features

The above example tells how you can register WordPress Custom Post Types to the back-end for any theme. Now it’s time to move to the next step. Creating posts as a custom post type.

Create  a New Post

Let’s assume that you have successfully created a post for your WordPress website. Now you want to add this post as a custom post type on your WordPress website. After that, add two to three dummy News post to your site.

Create Post

Create a Template and Fetching List

Once, you have developed the code, your next task will be to create a new file called template-news.php and place it in your theme folder. As soon as you have created this file, add the following code to it.

<?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

Now create a new page called News from the Pages option in your WordPress dashboard and access it. You can see a Template option available in Page Attributes on the right side of your screen. Select the new template News and then click the update button. For further reference,  check out the image below.

Creating page

The result of Listing Page: The end result of how your listing page will be displayed on your website is provided below:

Listing Page Result

Add Menu for Custom Post Type

To add your new custom post type as a part of the Menu options on your WordPress website, navigate to Appearance → Menus and add the News page to your main menu. This step is necessary as it will display a navigational link to our newly created WordPress custom post type, News.

For further reference, check out the image below.

Adding Menu

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

Page result

Display Detail Page of Custom Post Type

We also need to create a detail page for custom post types. To do so, we just need to add a new file called single-news.php which is located in your WordPress theme and then add the following code to it.

<?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 how your detail page looks like:

Displaying the Detail Page

Wrapping up!

Well, you have now learned the way to create a Custom Post Type in WordPress, a pretty complex thing to do. If you have any query or suggestion feel free to ask me, I would love to reply you back. However, hosting WordPress on Cloudways is convenient. Just deploy secure and scalable cloud servers within minutes. Click the banner below to start your free trial.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Start Growing with Cloudways Today!

We never compromise on performance, security, and support.

Saud Razzak

Saud is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform. Saud is responsible for creating buzz, spread knowledge, and educate the people about WordPress in the Community around the globe. In his free time, he likes to play cricket and learn new things on the Internet. You can email him at m.saud@cloudways.com

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

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!