Join WhatsApp ChannelJoin Now

How to create custom post_type and taxonomy in WordPress

Hi Dev,

Today, we will show you How to create custom post_type and taxonomy in WordPress. This article will give you simple example of How to create custom post_type and taxonomy in WordPress. Let’s discuss How to create custom post_type and taxonomy in WordPress. In this article, we will implement a How to create custom post_type and taxonomy in WordPress.

So let’s follow few step to create example of How to create custom post_type and taxonomy in WordPress.

Step 1: function.php file open and add code

All you need to do is go to theme and open function.php file and copy the below code and paste it at the bottom of function.php file

/****************************************
 * Add custom taxonomy for Codeplaners *
 ****************************************/
add_action('init', function() {
	register_post_type('codeplaner', [
		'label' => __('CodePlaners', 'txtdomain'),
		'public' => true,
		'menu_position' => 5,
		'menu_icon' => 'dashicons-book',
		'supports' => ['title', 'editor', 'thumbnail', 'author', 'revisions', 'comments'],
		'show_in_rest' => true,
		'rewrite' => ['slug' => ''],
		'taxonomies' => ['codeplaner_author', 'codeplaner_category'],
		'labels' => [
			'singular_name' => __('Codeplaner', 'txtdomain'),
			'add_new_item' => __('Add new codeplaner', 'txtdomain'),
			'new_item' => __('New codeplaner', 'txtdomain'),
			'view_item' => __('View codeplaner', 'txtdomain'),
			'not_found' => __('No CodePlaners found', 'txtdomain'),
			'not_found_in_trash' => __('No CodePlaners found in trash', 'txtdomain'),
			'all_items' => __('All CodePlaners', 'txtdomain'),
			'insert_into_item' => __('Insert into codeplaner', 'txtdomain')
		],		
	]);
 
	register_taxonomy('codeplaner_category', ['codeplaner'], [
		'label' => __('categoryes', 'txtdomain'),
		'hierarchical' => true,
		'rewrite' => ['slug' => 'codeplaner-category'],
		'show_admin_column' => true,
		'show_in_rest' => true,
		'labels' => [
			'singular_name' => __('Category', 'txtdomain'),
			'all_items' => __('All categoryes', 'txtdomain'),
			'edit_item' => __('Edit Category', 'txtdomain'),
			'view_item' => __('View Category', 'txtdomain'),
			'update_item' => __('Update Category', 'txtdomain'),
			'add_new_item' => __('Add New Category', 'txtdomain'),
			'new_item_name' => __('New Category Name', 'txtdomain'),
			'search_items' => __('Search categoryes', 'txtdomain'),
			'parent_item' => __('Parent Category', 'txtdomain'),
			'parent_item_colon' => __('Parent Category:', 'txtdomain'),
			'not_found' => __('No categoryes found', 'txtdomain'),
		]
	]);
	register_taxonomy_for_object_type('codeplaner_category', 'codeplaner');
 

});

I hope it will assist you…

Recommended Posts