Skip to main content

How to Create Custom Post Types in WordPress

 We only get three built-in content types at the backend when we install WordPress, i.e., posts, pages, and media. However, today WordPress has become quite flexible and advanced.

The approach to adding more post types also has expanded. 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?

Custom post types are used to convert a regular WordPress website into a content management system. As the name suggests, you can use custom post types to create various content types for your website.

Besides that, one can find several post types available by default in WordPress installation, including:

  • Post – blog post
  • Page – static page
  • Attachment – attached media
  • Revision – post revision
  • Navigation Menu – nav menu
  • WordPress supports an unlimited number of Custom Post Types. You can create your custom posts and call them up wherever you want.
  • For example, if you run a News website and want 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. Additionally, you can create multiple post types, such as Movies, Portfolios, etc.

What Is a WordPress Custom Post Type?

Custom post types are used to convert a regular WordPress website into a content management system. As the name suggests, you can use custom post types to create various content types for your website.

Besides that, one can find several post types available by default in WordPress installation, including:

  • Post – blog post
  • Page – static page
  • Attachment – attached media
  • Revision – post revision
  • Navigation Menu – nav menu
  • WordPress supports an unlimited number of Custom Post Types. You can create your custom posts and call them up wherever you want.

    For example, if you run a News website and want 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. Additionally, you can create multiple post types, such as Movies, Portfolios, etc.

  • Follow the steps below to create a custom post type on a WordPress website:

    • Navigate to the function.php file from your WordPress theme directory
    • Add the following code to the function.php file
    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 */
    • Once you’ve added the code, the News post-type will automatically appear in the admin area

    Creating a Custom 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
    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 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.

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

    Before adding custom post features on WordPress

    – Before adding features

    After adding custom post features on WordPress
    – After adding features

    The steps above tell how to register WordPress custom post types to the backend of any theme. Now let’s learn how to add a custom post on a WordPress website.

    How to Create a New Custom Post on a WordPress Website

    You can create a new custom post on your WordPress website by following the steps below:

    • Click the 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 the Publish button to take the new custom post live.

    Create a New Custom Post on a WordPress Website

    How to Create a Template and Fetching List

    Creating a template and fetching a list requires creating a new file named template-news.php.

    • Place the newly created file in your theme folder.
    • Add the following code to the file:
    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

    • Go to your WordPress dashboard
    • Click Pages > Add 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 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

    How to Add Menu for Custom Post Type

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

    • Go to your WordPress dashboard
    • Navigate to Appearance > Menus
    • 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

  • How to 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:
    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 what your detail page looks like:

    Detail Page for Custom Post Type

    Wrapping Up!

    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. Check out our other blog posts for more helpful guides on managing your WordPress site, such as reducing WordPress load times. Shall you have any queries or suggestions regarding the blog, feel free to comment below.

Comments

Popular Posts

How to find out Max Salary from each department

You can find maximum salary for each department by grouping all records by DeptId and then using MAX() function to calculate maximum salary in each group or each department. SQL Query: SELECT DeptID, MAX(Salary) FROM Employee  GROUP BY DeptID. This questions become more interesting if Interviewer will ask you to print department name instead of department id, in that case you need to join Employee table with Department using foreign key DeptID, make sure you do LEFT or RIGHT OUTER JOIN to include departments without any employee as well.  Here is the query