How to Create a Custom Post Types in WordPress

wordpress custom post type

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 more content types because posts, pages, and media are not enough and here is where WordPress custom post type comes in handy.

  • What is a WordPress Custom Post Type?
  • Create a WordPress Custom Post Type
  • Create a New Post
  • Create a Template and Fetching List
  • Select a Template
  • Add Menu for a Custom Post Type
  • Display Detail Page of Custom Post Type

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 types 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.

  1. /* Custom Post Type Start */
  2. function create_posttype() {
  3. register_post_type( ‘news’,
  4. // CPT Options
  5. array(
  6. ‘labels’ => array(
  7. ‘name’ => __( ‘news’ ),
  8. ‘singular_name’ => __( ‘News’ )
  9. ),
  10. ‘public’ => true,
  11. ‘has_archive’ => false,
  12. ‘rewrite’ => array(‘slug’ => ‘news’),
  13. )
  14. );
  15. }
  16. // Hooking up our function to theme setup
  17. add_action( ‘init’, ‘create_posttype’ );
  18. /* 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.

  1. /*Custom Post type start*/
  2. function cw_post_type_news() {
  3. $supports = array(
  4. ‘title’, // post title
  5. ‘editor’, // post content
  6. ‘author’, // post author
  7. ‘thumbnail’, // featured images
  8. ‘excerpt’, // post excerpt
  9. ‘custom-fields’, // custom fields
  10. ‘comments’, // post comments
  11. ‘revisions’, // post revisions
  12. ‘post-formats’, // post formats
  13. );
  14. $labels = array(
  15. ‘name’ => _x(‘news’, ‘plural’),
  16. ‘singular_name’ => _x(‘news’, ‘singular’),
  17. ‘menu_name’ => _x(‘news’, ‘admin menu’),
  18. ‘name_admin_bar’ => _x(‘news’, ‘admin bar’),
  19. ‘add_new’ => _x(‘Add New’, ‘add new’),
  20. ‘add_new_item’ => __(‘Add New news’),
  21. ‘new_item’ => __(‘New news’),
  22. ‘edit_item’ => __(‘Edit news’),
  23. ‘view_item’ => __(‘View news’),
  24. ‘all_items’ => __(‘All news’),
  25. ‘search_items’ => __(‘Search news’),
  26. ‘not_found’ => __(‘No news found.’),
  27. );
  28. $args = array(
  29. ‘supports’ => $supports,
  30. ‘labels’ => $labels,
  31. ‘public’ => true,
  32. ‘query_var’ => true,
  33. ‘rewrite’ => array(‘slug’ => ‘news’),
  34. ‘has_archive’ => true,
  35. ‘hierarchical’ => false,
  36. );
  37. register_post_type(‘news’, $args);
  38. }
  39. add_action(‘init’, ‘cw_post_type_news’);
  40. /*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 posts 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.

  1. <?php
  2. /*Template Name: News*/
  3. get_header();
  4. query_posts(array(
  5. ‘post_type’ => ‘news’
  6. )); ?>
  7. <?php
  8. while (have_posts()) : the_post(); ?>
  9. <h2><a href=“<?php the_permalink() ?>”><?php the_title(); ?></a></h2>
  10. <p><?php the_excerpt(); ?></p>
  11. <?php endwhile;
  12. get_footer();
  13. ?>

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.

  1. <?php
  2. get_header();
  3. /* Start the Loop */
  4. while (have_posts()) : the_post();
  5. get_template_part(‘template-parts/post/content’, get_post_format());
  6. endwhile; // End of the loop.
  7. get_footer();
  8. ?>

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

Displaying the Detail Page