How to create custom post type using the custom code

  • April 23, 2022
  • 0
Hire Vivek Asodariya

What is the use of custom post type? 

Using the custom post type, we can manage the WordPress site content. We can create multiple custom post types. When you have multiple plugins on the site and you don’t increase the site load. That time you can create a custom post type using the custom code.

In WordPress, when we need to create a custom post type then you can add the below code.

You can create your own custom post type and set the post type name.

We need to require a Custom Cake post type so I have created Custom Cake  Post type with taxonomy.

All users’ websites are different such as some websites are movie-related, and some websites are book related.
So User needs to create his own website-related post type.

Add the below code in your (child) theme’s functions.php 

If your website does not have a child theme, you can use the snippet plugin to add custom code.
And if you don’t want to use the snippet plugin, you can create your own plugin and you can add custom code to that plugin.

<?php

function custom_cake_post_type() {
    $args = array(
                  'labels' => array(
                                    'name'=> 'Custom Cakes',
                                    'singular_name' => 'Custom Cake',
                                    'add_new' => 'Add New Custom Cake',
                                    'add_new_item' => 'Add New Custom Cake',
                                    'edit' => 'Edit Custom Cake',
                                    'edit_item' => 'Edit Custom Cake',
                                    'new-item' => 'New Custom Cake',
                                    'view' => 'View Custom Cake',
                                    'view_item' => 'View Custom Cake',
                                    'search_items' => 'Search Custom Cake',
                                    'not_found' => 'No Custom Cake Found',
                                    'not_found_in_trash' => 'No Custom Cake Found in Trash',
                                    'parent' => 'Parent Custom Cake'                                    
                                    
                                   ),
				'description' => 'Custom Cake',
				'public'              => true,
				'hierarchical' => true,
				'exclude_from_search' => false,
				'publicly_queryable'  => true,
				'show_ui'             => true,
				'show_in_menu'        => true,
				'show_in_nav_menus'   => true,
				'show_in_admin_bar'   => true,
                'supports' => array('title', 'editor', 'thumbnail' , 'excerpt'),
				'taxonomies' => array( 'custom_cake_category'),
				'has_archive' => true,
				'rewrite' => array('slug' => 'custom-cake-delivery'),
				'can_export'          => true,
                 );
    register_post_type('custom_cake' , $args );
}
add_action( 'init', 'custom_cake_post_type', 0 );
add_action( 'init', 'create_custom_cake_taxonomy', 0 );

function create_custom_cake_taxonomy() {
  $labels = array(
    'name' => _x( 'Custom Cake Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Custom Cake Category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Custom Cake Categories' ),
    'all_items' => __( 'All Custom Cake Categories' ),
    'parent_item' => __( 'Parent Custom Cake Category' ),
    'parent_item_colon' => __( 'Parent Custom Cake Category:' ),
    'edit_item' => __( 'Edit Custom Cake Category' ), 
    'update_item' => __( 'Update Custom Cake Category' ),
    'add_new_item' => __( 'Add New Custom Cake Category' ),
    'new_item_name' => __( 'New Custom Cake Category Name' ),
    'menu_name' => __( 'Custom Cake Categories' ),
  );    
  register_taxonomy('custom_cake_category',array('custom_cake'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'custom-cake-delivery-category', 'with_front' => false, 'hierarchical' => true  )
  ));
 
}

Once the code above is added, you can type your custom post type in the dashboard menu of WordPress and from there you can add your post.

Leave a Reply

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