How to create custom post type with category in wordpress?

To create a custom post type with categories in WordPress, follow these steps:

  1. Register the Custom Post Type

    Add the following code to your theme’s functions.php file or a custom plugin:

    
    function my_custom_post_type() {
        $labels = array(
            'name'                  => _x('Books', 'Post type general name', 'textdomain'),
            'singular_name'         => _x('Book', 'Post type singular name', 'textdomain'),
            'menu_name'             => _x('Books', 'Admin Menu text', 'textdomain'),
            'name_admin_bar'        => _x('Book', 'Add New on Toolbar', 'textdomain'),
            'add_new'               => __('Add New', 'textdomain'),
            'add_new_item'          => __('Add New Book', 'textdomain'),
            'new_item'              => __('New Book', 'textdomain'),
            'edit_item'             => __('Edit Book', 'textdomain'),
            'view_item'             => __('View Book', 'textdomain'),
            'all_items'             => __('All Books', 'textdomain'),
            'search_items'          => __('Search Books', 'textdomain'),
            'parent_item_colon'     => __('Parent Books:', 'textdomain'),
            'not_found'             => __('No books found.', 'textdomain'),
            'not_found_in_trash'    => __('No books found in Trash.', 'textdomain'),
        );
    
        $args = array(
            'labels'                => $labels,
            'public'                => true,
            'publicly_queryable'    => true,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'query_var'             => true,
            'rewrite'               => array('slug' => 'books'),
            'capability_type'       => 'post',
            'has_archive'           => true,
            'hierarchical'          => false,
            'menu_position'         => 5,
            'supports'              => array('title', 'editor', 'thumbnail'),
        );
    
        register_post_type('book', $args);
    }
    add_action('init', 'my_custom_post_type');
    			
    		

    books post

  2. Register Custom Taxonomy (Category)

    Add the following code to create a custom taxonomy for the custom post type:

    
    function my_custom_taxonomy() {
        $labels = array(
            'name'              => _x('Book Categories', 'taxonomy general name', 'textdomain'),
            'singular_name'     => _x('Book Category', 'taxonomy singular name', 'textdomain'),
            'search_items'      => __('Search Book Categories', 'textdomain'),
            'all_items'         => __('All Book Categories', 'textdomain'),
            'parent_item'       => __('Parent Book Category', 'textdomain'),
            'parent_item_colon' => __('Parent Book Category:', 'textdomain'),
            'edit_item'         => __('Edit Book Category', 'textdomain'),
            'update_item'       => __('Update Book Category', 'textdomain'),
            'add_new_item'      => __('Add New Book Category', 'textdomain'),
            'new_item_name'     => __('New Book Category Name', 'textdomain'),
            'menu_name'         => __('Book Categories', 'textdomain'),
        );
    
        $args = array(
            'hierarchical'      => true, // Set to true for category-like behavior
            'labels'            => $labels,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => array('slug' => 'book-category'),
        );
    
        register_taxonomy('book_category', array('book'), $args);
    }
    add_action('init', 'my_custom_taxonomy');
    		

    books post

Summary

  • Custom Post Type: The register_post_type() function creates the new post type.
  • Custom Taxonomy: The register_taxonomy() function creates the custom taxonomy (category) for the new post type.

After adding this code, you should see a new menu item for “Books” in your WordPress admin panel, and under “Books,” you should be able to see and manage the custom categories.

If you have more questions or need additional customization, let me know!

Post a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *