How To Add a Custom Post Type in WordPress

In WordPress, custom post types are an essential tool for creating and managing your own custom content, such as products, events, or services. By using custom post types, you can extend the functionality of WordPress and create more specific and customized content for your website. In this article, we will show you how to add a custom post type in WordPress using PHP code in the functions.php file.

First, open the functions.php file in your WordPress theme’s folder using a text editor. Then, add the following code to the functions.php file, replacing “my_post_type” with the name of your custom post type and “My Post Type” with the label for your custom post type:

function register_my_post_type() {
    $labels = [
        "name" => "My Post Type",
        "singular_name" => "My Post Type",
        "add_new" => "Add New",
        "add_new_item" => "Add New My Post Type",
        "edit_item" => "Edit My Post Type",
        "new_item" => "New My Post Type",
        "all_items" => "All My Post Type",
        "view_item" => "View My Post Type",
        "search_items" => "Search My Post Type",
        "not_found" => "No My Post Type found",
        "not_found_in_trash" => "No My Post Type found in Trash",
        "parent_item_colon" => "",
        "menu_name" => "My Post Type",
    ];

    $args = [
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_menu" => true,
        "query_var" => true,
        "rewrite" => ["slug" => "my-post-type"],
        "capability_type" => "post",
        "has_archive" => true,
        "hierarchical" => false,
        "menu_position" => null,
        "supports" => [
            "title",
            "editor",
            "author",
            "thumbnail",
            "excerpt",
            "comments",
        ],
    ];

    register_post_type("my_post_type", $args);
}

add_action("init", "register_my_post_type");

Once you have added the code to the functions.php file, save the changes and upload it to your WordPress theme’s folder. Then, go to the Posts > Add New page in your WordPress dashboard and verify that your custom post type is available in the “Post Type” dropdown menu. You can now create and manage your custom post type posts in WordPress.

In the code above, we have registered a basic custom post type with the default WordPress post fields and capabilities. However, you can customize the code to add additional features and options to your custom post type, such as custom fields, taxonomies, and custom post type icons.

You can also multiple custom post types under one menu.

To add a custom taxonomy to your custom post type, you can use the following code:

function add_my_taxonomy() {
    $labels = [
        "name" => "My Taxonomy",
        "singular_name" => "My Taxonomy",
        "search_items" => "Search My Taxonomy",
        "all_items" => "All My Taxonomy",
        "parent_item" => "Parent My Taxonomy",
        "parent_item_colon" => "Parent My Taxonomy:",
        "edit_item" => "Edit My Taxonomy",
        "update_item" => "Update My Taxonomy",
        "add_new_item" => "Add New My Taxonomy",
        "new_item_name" => "New My Taxonomy Name",
        "menu_name" => "My Taxonomy",
    ];

    $args = [
        "labels" => $labels,
        "hierarchical" => true,
        "show_ui" => true,
        "show_admin_column" => true,
        "query_var" => true,
        "rewrite" => ["slug" => "my-taxonomy"],
    ];

    register_taxonomy("my_taxonomy", "my_post_type", $args);
}
add_action("init", "add_my_taxonomy");

To use the custom taxonomy, simply edit a post in your custom post type and select the taxonomy terms in the “My Taxonomy” meta box. The taxonomy terms will be displayed on the front-end of your website, and users will be able to filter posts by taxonomy terms on the custom post type archive page.

In conclusion, custom post types are a powerful and flexible way to extend the functionality of WordPress and create custom content for your website. By following the steps above, you can easily add a custom post type and customize it with custom fields, taxonomies, and other features. With custom post types, you can create a unique and tailored user experience for your website and its users.

I hope you find this useful. Please comment if you have any questions.