How to create a custom post type in WordPress?

What is Post Type?

Post types are used for different types of content in WordPress CMS. For example, Post is used for blog posts (articles) & Pages are used to create different pages on the WordPress CMS.

What is a Custom Post type?

Same as the Post type, custom post types are used to create our own content post types in the WordPress CMS. By using a custom post type we can transform a simple WordPress website into a powerful Content Management System (CMS). We can create any number of custom post types as per our requirements like movies, projects, testimonials, etc.

Steps to create a custom post type without any plugin:

First, you need to check if your theme is free, paid, or custom theme. If your theme is free or paid then please check if a child theme is created or not, if not then create a child theme first.

Now for creating a custom post type we just need to add the required code in the functions.php file.

Here is the minimum code required to create a custom post type:

/**
 * Create a Custom Project Post Type
 */
function akmaurya_custom_post_type() {
    register_post_type('project',
        array(
            'labels'      => array(
                'name'          => __('Projects', 'akmaurya'),
                'singular_name' => __('Project', 'akmaurya'),
            ),
            'public'      => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'projects'),
        )
    );
}
add_action('init', 'akmaurya_custom_post_type');

The above code will add the Project custom post type option on the website backend. You can see this Project option in the dashboard sidebar.

The above code has two parts an array & labels which is also an array. The above code is just adding the project option in the backend with limited functionality.

Now let’s see the whole code which will add all the functionality of the post to our project custom post type.

Remove the above code from the functions.php file & add the full code mentioned below.

// Projects custom post type
// 

function akm_create_projects_posttype(){
	$args = array(
		'label' => 'Projects',
		'labels' => array(  // These all are the labels we see in the dashboard.
			'name' => __('Projects'),
			'singular_name' => __('Projects'),
			'add_new' => __('Add New'),
			'add_new_item' => __('Add New Projects'),
			'edit_item' => __('Edit Projects'),
			'new_item' => __('New Projects'),
			'view_item' => __('View Projects'),
			'search_items' => __('Search Projects'),
			'not_found' => __('No Projects found'),
			'not_found_in_trash' => __('No Projects found in trash'),
			'all_items' => __('All Projects'),
			'archives' => __('Projects Archives'),
			'insert_into_item' => __('Insert into Projects'),
			'uploaded_to_this_item' => __('Uploaded to this Projects'),
			'featured_image' => __('Projects Image'),
			'set_featured_image' => __('Set Projects Image'),
			'remove_featured_image' => __('Remove Projects Image'),
			'use_featured_image' => __('Use Projects Image'),
			'menu_name' => __('Projects')  // This is the menu name
		),
		'description' => __('Post type to contain content for Projects.'),
		'public' => true,
		'exclude_from_search' => false,
		'publicly_queryable' => true,
		'show_ui' => true,
		'show_in_nav_menus' => true,
		'show_in_menu' => true,
		'show_in_admin_bar' => true,
		'menu_position' => 18,
		'menu_icon' => 'dashicons-feedback',  // this is the menu icon in backend.
		'supports' => array('title','editor','thumbnail','excerpt','revisions','page-attributes', 'custom-fields'),
		'has_archive' => false,
		'hierarchical' => false,
		'rewrite' => ['slug'=>'project'],
		'capabilities' => array(
			'edit_post'          => 'update_core',
			'read_post'          => 'update_core',
			'delete_post'        => 'update_core',
			'edit_posts'         => 'update_core',
			'edit_others_posts'  => 'update_core',
			'delete_posts'       => 'update_core',
			'publish_posts'      => 'update_core',
			'read_private_posts' => 'update_core'
		),
	);
	register_post_type('project',$args);
}
add_action('init','akm_create_projects_posttype',0);

Now you can see we have added lot more codes in the array. The above code will add more functionality support to your custom post type like, support for revisions, featured images, custom fields, and more.

Leave a Comment

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

Scroll to Top