Unleash your WordPress website's potential with custom post types. Learn how to create and customize them to organize and present diverse content in a way that perfectly aligns with your unique needs.
Understanding What Is Custom Post Type in WordPress?
A custom post type in WordPress refers to a content type that you can create and define according to your specific needs and requirements. While WordPress comes with built-in post types like posts and pages, custom post types enable you to extend the functionality of your website by introducing new content types tailored to your unique purposes.
Built-in post types
Posts: Posts are the primary content type used for creating blog entries or articles. They are typically displayed in reverse chronological order on the blog page and can be organized using categories and tags.
Pages: Pages are static content types used for creating non-chronological and hierarchical content such as About Us, Contact, or Services pages. Pages are typically displayed in the site's navigation menu and are not associated with dates or categories.
Attachments: Attachments represent files, such as images or documents, that are attached to a post or page. They can be displayed as part of the content or linked for download.
Revisions: Revisions are not visible on the front end of the website but are automatically created whenever a post or page is saved or updated. Revisions allow you to revert back to previous versions of your content if needed.
Custom post types allow you to organize and display different types of content separately, making it easier to manage and present diverse information effectively. Whether you want to showcase portfolios, testimonials, events, products, or any other specialized content, custom post types provide a flexible and efficient way to structure and handle such data. By defining custom fields, taxonomies, and other settings, you can fully customize the behavior, appearance, and functionality of these post types to align with your specific objectives and deliver an enhanced user experience.
Read: How to Add a Blog Post
Why Creating Custom Post Type is Necessary
You may need to utilize custom post types in your WordPress site in various situations. Here are some scenarios where custom post types can be beneficial:
1. Organizing Different Content
If your website contains diverse content that doesn't fit neatly into the standard post or page structure, custom post types allow you to create separate content containers tailored to specific types of information. For example, if you have a portfolio section, testimonial collection, or event listings, custom post types provide a structured way to manage and display these distinct content types.
2. Enhancing Content Management
Custom post types offer improved content management capabilities. By defining custom fields and taxonomies, you can capture and categorize specific information for each post type. This allows for better organization, searchability, and filtering of content, making it easier to manage and find relevant information within your site's backend.
3. Building Specific Features
If your website requires unique functionality, custom post types can serve as the foundation for implementing those features. Whether it's a real estate listing directory, product catalog, or knowledge base, custom post types enable you to define the specific attributes, templates, and behavior needed to showcase and manage such specialized content.
4. Content Presentation and Styling
Custom post types provide more control over how your content is presented and styled on the front end. You can design custom templates and layouts for each post type, allowing for tailored display options that align with the specific content's structure and purpose.
Read: Understanding WordPress Themes
How to Create Custom Post Types Manually with Code
First, Open WordPress Admin Panel then go to Appearance > Theme File Editor
Choose your current theme from Select the to edit drop-down menu. Then click on the function.php file.
Now add this code in the function.php file and press the Update File button.
For Example Here “Stories” is used as Custom Post Type
/* Custom Post Type Begins */
function create_posttype() {
register_post_type( 'Stories',
// CPT Options
array(
'labels' => array(
'name' => __( 'Stories' ),
'singular_name' => __( 'Story' )
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'Stories'),
)
);
}
// Hooking up this function to current theme setup
add_action( 'init', 'create_posttype' );
/* Custom Post Type End */
In the left-side menu, a new post type will appear as “Stories”. Click on it or hover over it and press “Add New” to start a new post from this new Custom Post Type.
The rest of the process is the same as normal post-creation.
For including more additional options within Custom Post Type, use the following code instead. It adds features like revisions, featured images, custom fields, and associates the post type with a custom taxonomy called 'genres.'
Caution: Avoid combining the code above and written below together to prevent errors. Follow the same rule to enter the function.php file and use the codes separately. As per example Story and Stories are used in the code. Replace the codes with your personal Custom Post Type.
/* Custom Post Type Beginnes */
function cw_post_type_stories() {
$supports = array(
'title', // post title
'editor', // post content
'author', // post author
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
'comments', // post comments
'revisions', // post revisions
'post-formats', // post formats
);
$labels = array(
'name' => _x('Stories', 'plural'),
'singular_name' => _x('Story', 'singular'),
'menu_name' => _x('Stories', 'admin menu'),
'name_admin_bar' => _x('Stories', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New Story'),
'new_item' => __('New Story'),
'edit_item' => __('Edit Story'),
'view_item' => __('View Story'),
'all_items' => __('All Stories'),
'search_items' => __('Search Stories'),
'not_found' => __('No stories found.'),
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'stories'),
'has_archive' => true,
'hierarchical' => false,
);
register_post_type('stories', $args);
}
add_action('init', 'cw_post_type_stories');
/* Custom Post Type End */
It's worth mentioning that the hierarchical value is set to false in the code. However, if you prefer your custom post type to function similarly to Pages instead of Posts, you can modify this value to true.
Before adding the additional code new post screen looks like this -
After adding the additional code the post screen looks like this -
How to Create Custom Post Types by Using Plugin
First, Install and Activate the Custom Post Type UI plugin.
Then navigate to CPT UI > Add/Edit Post Types
Now from the Basic Settings portion Input the Post Type Slug, Plural Label, and Singular Label, and finally click on Add Post Type button.
For example, we are using “ Story” here.
A success message will be shown and in the left-side menu, you will see this new post type.
Scroll down to find various customization options for your custom post type. Among them, locate the Hierarchical option and set it to True from the dropdown menu.
Setting the hierarchical value to false makes the custom post type behave like regular posts. Setting it to true allows for hierarchical relationships, similar to pages. Choose based on the desired content structure.
How to Create a New Custom Post
Creating a post in a custom post type follows the same process as a regular post. Simply hover over the custom post. Referred to as "Stories" in this example. Now, click on the "Add New." button.
Enter the post details, including the title, description, and images. Take necessary actions for the post. You can save it as a draft and Preview it before publishing. The option of Updating the content is also the same for these custom-type posts.
How to Display Custom Post Types
To add a custom link to your menu for your Custom Post Type, go to Appearance » Menus. In Menu Structure scroll down to the Custom Links drop-down menu and paste the URL of your new Post type. Next, write your Custom Type Post name in the Link Text sector. Finally, Click on Save Menu to complete your upgrade.
Final Words
Custom post types in WordPress provide flexibility to organize and present diverse content. By creating and customizing these post types, you can enhance your website's organization, streamline content management, and deliver a unique user experience. Embrace custom post types to unlock your site's full potential and create a dynamic platform that engages your audience.