{"id":1583,"date":"2024-09-11T04:38:54","date_gmt":"2024-09-11T04:38:54","guid":{"rendered":"https:\/\/debugspot.com\/?p=1583"},"modified":"2024-09-11T04:39:14","modified_gmt":"2024-09-11T04:39:14","slug":"how-to-create-custom-post-type-with-category-in-wordpress","status":"publish","type":"post","link":"https:\/\/debugspot.com\/blogs\/how-to-create-custom-post-type-with-category-in-wordpress\/","title":{"rendered":"How to create custom post type with category in wordpress?"},"content":{"rendered":"<p>To create a custom post type with categories in WordPress, follow these steps:<\/p>\n<ol>\n<li>\n\t\t<strong>Register the Custom Post Type<\/strong><\/p>\n<p>Add the following code to your theme&#8217;s functions.php file or a custom plugin:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\">\r\nfunction my_custom_post_type() {\r\n    $labels = array(\r\n        'name'                  => _x('Books', 'Post type general name', 'textdomain'),\r\n        'singular_name'         => _x('Book', 'Post type singular name', 'textdomain'),\r\n        'menu_name'             => _x('Books', 'Admin Menu text', 'textdomain'),\r\n        'name_admin_bar'        => _x('Book', 'Add New on Toolbar', 'textdomain'),\r\n        'add_new'               => __('Add New', 'textdomain'),\r\n        'add_new_item'          => __('Add New Book', 'textdomain'),\r\n        'new_item'              => __('New Book', 'textdomain'),\r\n        'edit_item'             => __('Edit Book', 'textdomain'),\r\n        'view_item'             => __('View Book', 'textdomain'),\r\n        'all_items'             => __('All Books', 'textdomain'),\r\n        'search_items'          => __('Search Books', 'textdomain'),\r\n        'parent_item_colon'     => __('Parent Books:', 'textdomain'),\r\n        'not_found'             => __('No books found.', 'textdomain'),\r\n        'not_found_in_trash'    => __('No books found in Trash.', 'textdomain'),\r\n    );\r\n\r\n    $args = array(\r\n        'labels'                => $labels,\r\n        'public'                => true,\r\n        'publicly_queryable'    => true,\r\n        'show_ui'               => true,\r\n        'show_in_menu'          => true,\r\n        'query_var'             => true,\r\n        'rewrite'               => array('slug' => 'books'),\r\n        'capability_type'       => 'post',\r\n        'has_archive'           => true,\r\n        'hierarchical'          => false,\r\n        'menu_position'         => 5,\r\n        'supports'              => array('title', 'editor', 'thumbnail'),\r\n    );\r\n\r\n    register_post_type('book', $args);\r\n}\r\nadd_action('init', 'my_custom_post_type');\r\n\t\t\t<\/code>\r\n\t\t<\/pre>\n<p>\t\t<img decoding=\"async\" src=\"https:\/\/debugspot.com\/blogs\/wp-content\/uploads\/2024\/09\/books-post.png\" width=\"\" height=\"\" alt=\"books post\" \/>\n\t<\/li>\n<li>\n\t\t<strong>Register Custom Taxonomy (Category)<\/strong><\/p>\n<p>Add the following code to create a custom taxonomy for the custom post type:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\">\r\nfunction my_custom_taxonomy() {\r\n    $labels = array(\r\n        'name'              => _x('Book Categories', 'taxonomy general name', 'textdomain'),\r\n        'singular_name'     => _x('Book Category', 'taxonomy singular name', 'textdomain'),\r\n        'search_items'      => __('Search Book Categories', 'textdomain'),\r\n        'all_items'         => __('All Book Categories', 'textdomain'),\r\n        'parent_item'       => __('Parent Book Category', 'textdomain'),\r\n        'parent_item_colon' => __('Parent Book Category:', 'textdomain'),\r\n        'edit_item'         => __('Edit Book Category', 'textdomain'),\r\n        'update_item'       => __('Update Book Category', 'textdomain'),\r\n        'add_new_item'      => __('Add New Book Category', 'textdomain'),\r\n        'new_item_name'     => __('New Book Category Name', 'textdomain'),\r\n        'menu_name'         => __('Book Categories', 'textdomain'),\r\n    );\r\n\r\n    $args = array(\r\n        'hierarchical'      => true, \/\/ Set to true for category-like behavior\r\n        'labels'            => $labels,\r\n        'show_ui'           => true,\r\n        'show_admin_column' => true,\r\n        'query_var'         => true,\r\n        'rewrite'           => array('slug' => 'book-category'),\r\n    );\r\n\r\n    register_taxonomy('book_category', array('book'), $args);\r\n}\r\nadd_action('init', 'my_custom_taxonomy');\r\n\t\t<\/code><\/pre>\n<p>\t\t<img decoding=\"async\" src=\"https:\/\/debugspot.com\/blogs\/wp-content\/uploads\/2024\/09\/book-category.png\" width=\"\" height=\"\" alt=\"books post\" \/>\n\t<\/li>\n<\/ol>\n<h1 class=\"post-heading\"> Summary <\/h1>\n<ul>\n<li><strong>Custom Post Type:<\/strong> The register_post_type() function creates the new post type.<\/li>\n<li><strong>Custom Taxonomy:<\/strong> The register_taxonomy() function creates the custom taxonomy (category) for the new post type.<\/li>\n<\/ul>\n<p>After adding this code, you should see a new menu item for &#8220;Books&#8221; in your WordPress admin panel, and under &#8220;Books,&#8221; you should be able to see and manage the custom categories.<\/p>\n<p>If you have more questions or need additional customization, let me know!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To create a custom post type with categories in WordPress, follow these steps: Register the Custom Post Type Add the following code to your theme&#8217;s functions.php file or a custom plugin: function my_custom_post_type() { $labels = array( &#8216;name&#8217; => _x(&#8216;Books&#8217;, &#8216;Post type general name&#8217;, &#8216;textdomain&#8217;), &#8216;singular_name&#8217; => _x(&#8216;Book&#8217;, &#8216;Post type singular name&#8217;, &#8216;textdomain&#8217;), &#8216;menu_name&#8217; => _x(&#8216;Books&#8217;, &#8216;Admin Menu text&#8217;, &#8216;textdomain&#8217;), &#8216;name_admin_bar&#8217; => _x(&#8216;Book&#8217;, &#8216;Add New on Toolbar&#8217;, &#8216;textdomain&#8217;), &#8216;add_new&#8217; => __(&#8216;Add New&#8217;, &#8216;textdomain&#8217;), &#8216;add_new_item&#8217; => __(&#8216;Add New Book&#8217;, &#8216;textdomain&#8217;), &#8216;new_item&#8217; => __(&#8216;New Book&#8217;, &#8216;textdomain&#8217;), &#8216;edit_item&#8217; => __(&#8216;Edit Book&#8217;, &#8216;textdomain&#8217;), &#8216;view_item&#8217; => __(&#8216;View Book&#8217;, &#8216;textdomain&#8217;), &#8216;all_items&#8217; => __(&#8216;All Books&#8217;, &#8216;textdomain&#8217;), &#8216;search_items&#8217; => __(&#8216;Search Books&#8217;, &#8216;textdomain&#8217;), &#8216;parent_item_colon&#8217; => __(&#8216;Parent Books:&#8217;, &#8216;textdomain&#8217;), &#8216;not_found&#8217; => __(&#8216;No books found.&#8217;, &#8216;textdomain&#8217;), &#8216;not_found_in_trash&#8217; => __(&#8216;No books found in Trash.&#8217;, &#8216;textdomain&#8217;), ); $args = array( &#8216;labels&#8217; => $labels, &#8216;public&#8217; => true, &#8216;publicly_queryable&#8217; => true, &#8216;show_ui&#8217; => true, &#8216;show_in_menu&#8217; => true, &#8216;query_var&#8217; => true, &#8216;rewrite&#8217; => array(&#8216;slug&#8217; => &#8216;books&#8217;), &#8216;capability_type&#8217; => &#8216;post&#8217;, &#8216;has_archive&#8217; => true, &#8216;hierarchical&#8217; => false, &#8216;menu_position&#8217; => 5, &#8216;supports&#8217; => array(&#8216;title&#8217;, &#8216;editor&#8217;, &#8216;thumbnail&#8217;), ); register_post_type(&#8216;book&#8217;, $args); } add_action(&#8216;init&#8217;, &#8216;my_custom_post_type&#8217;); Register Custom Taxonomy (Category) Add the following code to create a custom taxonomy for the custom post type: function my_custom_taxonomy() { $labels = array( &#8216;name&#8217; => _x(&#8216;Book &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"rank_math_lock_modified_date":false,"footnotes":""},"categories":[29],"tags":[56,57,55],"class_list":["post-1583","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-create-custom-post-type","tag-create-custom-post-type-with-category","tag-wordpress"],"acf":[],"_links":{"self":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1583"}],"collection":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/comments?post=1583"}],"version-history":[{"count":1,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1583\/revisions"}],"predecessor-version":[{"id":1586,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1583\/revisions\/1586"}],"wp:attachment":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/media?parent=1583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/categories?post=1583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/tags?post=1583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}