Creating custom post type (CPT) in WordPress allows you to organize your content in a more structured way, especially if you’re building a specialized website (e.g., a portfolio, product listings, or event management). You can create custom post type without using a plugin, simply by adding code to your theme’s functions.php
file. This method gives you full control over your custom post types.
IMPORTANT: Before making any changes to your site’s code, it is highly recommended that you backup your website. This is especially important if you are a beginner and unfamiliar with editing code.
Let’s dive into it!
Steps to Create Custom Post Type Without a Plugin
Follow these simple steps to create custom post type:
- Access your WordPress Theme’s
functions.php
file via FTP client (such as FileZilla) (recommended). - Add the Code to Register the Custom Post Type:
function jawad_register_custom_post_type() {
$args = array(
'label' => 'Projects', // Name of the post type
'public' => true, // Whether the post type is public
'hierarchical' => true, // If true, allows parent-child relationships
'supports' => array('title', 'editor', 'thumbnail'), // Features the post type supports
'menu_position' => 5, // Position in the admin menu
'menu_icon' => 'dashicons-portfolio', // Icon for the admin menu
'show_in_rest' => true, // Enable the post type for Gutenberg editor
'rewrite' => array('slug' => 'projects'), // URL structure for the post type
'has_archive' => true, // Whether to enable archives for this post type
'show_ui' => true, // Display the post type in the admin panel
);
// Register the custom post type
register_post_type('project', $args);
}
add_action('init', 'jawad_register_custom_post_type');
Breakdown of Each Parameter
label
: The display name of your custom post type. In this case, it’s ‘Projects’, which will be shown in the WordPress admin dashboard.public
: This defines whether the post type is publicly accessible or not. Set totrue
, meaning users can see it on your website.hierarchical
: Set totrue
for hierarchical post types, like Pages, where you can have parent and child posts. Set tofalse
for non-hierarchical post types, like Posts.supports
: Specifies the WordPress features you want to support for your post type. In this example, the post type will support thetitle
,editor
, andthumbnail
fields.menu_position
: Defines the position of your custom post type in the WordPress admin menu. Setting it to5
places it right below the Pages menu.menu_icon
: The icon used for your custom post type in the WordPress admin menu. In this case, it’s set to a portfolio icon (dashicons-portfolio
).show_in_rest
: This parameter is set totrue
to enable the block editor (Gutenberg) for your custom post type. This is essential for modern editing features.rewrite
: Controls the permalink structure for your custom post type. Here, the slug is set to ‘projects’, so the URL for each post will look likeyoursite.com/projects/sample-post
.has_archive
: Set totrue
, this enables archive pages for your custom post type. For example, you could view all projects in a list format on a page likeyoursite.com/projects
.show_ui
: When set totrue
, this ensures that the custom post type is displayed in the WordPress admin panel. It allows admins to manage content from the backend.
Summary
By following these steps, you’ll have successfully created a custom post type called Projects without using any plugins. You can customize the parameters to suit your needs, like changing the supports
array to include features like custom fields, or adjusting the menu_icon
to use another Dashicon.
Custom post types are a powerful tool in WordPress, helping you organize content efficiently and creatively!
If you need any help, feel free to contact me.