How to create custom post type & taxonomy in WordPress

How to create custom post type & taxonomy in WordPress

Hello Readers!

WordPress is one of the popular CMS and 30% websites are created in it. This is because, it is very easy to manage your websites even if you don't have more technical knowledge.

For developer, WordPress is one of the best option for CMS. Beginner developers use plugins to create every functions which is not found in WordPress dashboard. I would like to guide how can they overcome to use of plugins for every functions. Plugins contains a lot of files and folders, while we need some line of codes for our requirements.

That's why I am sharing the code for creating custom post types, instead of using the plugins. Use of too many plugins makes our website slow and sometimes it conflicts when plugins is updated.

Before you create custom post type, let understand "What is Custom Post Type?".

WordPress comes with five default post types: postpageattachmentrevision, and menu.

Custom post types allow you to create different content types for your website. 

Steps of Creating Custom Post Type:

Step 1: Create a Child Theme.

First of all, create a child of your active theme. If you are not aware how to create child theme in WordPress, follow this link How To Create Child Theme in WordPress.

Now activate the child theme and check if your website is working fine.

Step 2: Write code in functions.php

Write following code in your child theme's functions.php file.

function custom_post_type_init() {
    $labels = array(
        'name' => 'Custom Post Type',
        'singular_name' => 'custom post type',
        'add_new' => 'Add New Post',
        'add_new_item' => 'Add New Post',
        'edit_item' => 'Edit Post',
        'new_item' => 'New Post',
        'all_items' => 'All Posts',
        'view_item' => 'View Post',
        'search_items' => 'Search Posts',
        'not_found' =>  'No Post Found',
        'not_found_in_trash' => 'No post found in Trash', 
        'parent_item_colon' => '',
        'menu_name' => 'Custom Post Type',
    );
    
    // register post type
    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'cudtom-post-type'),
        'query_var' => true,
        'menu_icon' => 'dashicons-admin-site',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes'
        )
    );
    register_post_type( 'cudtom-post-type', $args );
    
    // register taxonomy
    register_taxonomy(
        'custom-post-type-category', 
        'custom-post-type', 
        array(
            'hierarchical' => true, 
            'label' => 'Custom Post Type Category', 
            'query_var' => true, 
            'rewrite' => array( 'slug' => 'custom-post-type-category' )
        )
    );
}
add_action( 'init', 'custom_post_type_init' );        

Step 3: Replace the custom post type with your post type name.

Now rename the custom post type in above code with the name of your post type. Then save the functions.php file. Refresh the dashboard, you will notice a new menu appears in left side dashboard panel with name of your custom post type.

For further details to know, how to use custom post types, follow this link Working with Custom Post Types

I hope this tutorial helped you learn how to create custom post types in WordPress.

If you liked this article, then please subscribe to my YouTube Channel for development video tutorials. I will upload tutorial videos. You can also find me on Twitter, Instagram and Facebook.

To view or add a comment, sign in

More articles by Arvind Kumar

  • How to enable SVG support in WordPress

    WordPress support JPG and PNG file by default. But We can enable SVG support in WordPress without using any plugin.

  • Roadmap of Being a Web Developer

    Being a Web Developer is quite interesting and exciting. It was always fun for me when I started my career as a web…

  • How to create Child theme in WordPress

    Child theme is most important in wordPress. To add more functionality in your theme you need to create child theme, so…

  • Why HTML is must?

    Before we discuss this topic, let understand "What is HTML?". HTML - Hyper Text Markup Language ( if a non technical…

    2 Comments

Others also viewed

Explore content categories