A custom post type in WordPress is a feature that allows you to create your own post types in addition to the default post types (posts, pages, attachments, and Revision).
This feature enables you to create content that is specific to your website’s niche or purpose, which can make your website more organized and easier to navigate.
For example, if you are running a college website, you could create a custom post type for “Courses” that includes custom fields such as Name, Eligibility, and Fees. This allows you to display courses with their Eligibility and fees separately and organized.
Custom post types can be created using code or with help of the plugin.
In this blog, we will look at how can we create a custom post type in code.
- Inside Theme (functions.php)
- Inside mu-plugins (Must Use Plugin)
I do not recommend writing custom post-type code inside the functions.php file. This is because if you change your theme, you will lose custom post types.
Additionally, creating custom post types in the functions.php file can make your theme difficult to maintain. As your site grows, you may find that you need to add more custom post types or make changes to the existing ones. this becomes messy and difficult to manage if you have all of the code in your functions.php file.
Instead, it is recommended to create a Must-Use Plugin (mu-plugins) to create custom post types in WordPress.
Must-Use Plugins are loaded automatically by WordPress and cannot be deactivated from the admin panel, making them a good choice for custom functionality that needs to be always active.
To create a Must-Use plugin to add custom post types to your site, follow these steps:
- Create a new folder inside the wp-content/mu-plugins directory. If you do not have a `mu-plugins` folder create one the name should same as mentioned. inside this folder create folder anything you link.
- Inside this folder, create a new PHP file. You can name this file anything you like, but it should be a .php extension.
- Add your custom post code to this file.
function create_cources_post_type() {
register_post_type( 'cource',
array(
'labels' => array(
'name' => __( 'Cources' ),
'singular_name' => __( 'Cource' )
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-portfolio', // specify the icon
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ), // specify the supported properties
)
);
}
add_action( 'init', 'create_cources_post_type' );
These are just a few of the properties that can be used when creating a custom post type in WordPress. You can find a full list of available properties in the WordPress Codex.
If you want to add a custom icon to a Custom Post Type read this
Complete Example
To add Gutenberg compatibility in your custom post type, it require two things
1. supports must have editor in it
2. show_in_rest set to true
<?php
function custom_post_type_career() {
$labels = array(
'name' => 'Careers',
'singular_name' => 'Career',
'add_new' => 'Add New',
'add_new_item' => 'Add New Career',
'edit_item' => 'Edit Career',
'new_item' => 'New Career',
'view_item' => 'View Career',
'search_items' => 'Search Careers',
'not_found' => 'No careers found',
'not_found_in_trash' => 'No careers found in Trash',
'parent_item_colon' => 'Parent Career:',
'menu_name' => 'Careers'
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'careers' ),
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' ),
'menu_position' => 5,
'menu_icon' => 'dashicons-businessman',
);
register_post_type( 'career', $args );
}
add_action( 'init', 'custom_post_type_career' );
?>