Custom post types let you create new content types beyond posts and pages. This helps organize your site better.
0
0
Registering custom post types in Wordpress
Introduction
You want to add a portfolio section separate from blog posts.
You need a product catalog on your website.
You want to create event listings with special details.
You want to manage testimonials separately from regular posts.
Syntax
Wordpress
<?php
function create_custom_post_type() {
register_post_type('custom_type', [
'labels' => [
'name' => __('Custom Types'),
'singular_name' => __('Custom Type')
],
'public' => true,
'has_archive' => true,
'rewrite' => ['slug' => 'custom-types'],
'supports' => ['title', 'editor', 'thumbnail']
]);
}
add_action('init', 'create_custom_post_type');The register_post_type function creates the new type.
Hook it to init so WordPress loads it early.
Examples
This example creates a 'portfolio' post type for showcasing work.
Wordpress
<?php
function create_portfolio_post_type() {
register_post_type('portfolio', [
'labels' => [
'name' => __('Portfolios'),
'singular_name' => __('Portfolio')
],
'public' => true,
'has_archive' => true,
'supports' => ['title', 'editor', 'thumbnail']
]);
}
add_action('init', 'create_portfolio_post_type');This example creates an 'event' post type without archive pages but with custom fields support.
Wordpress
<?php
function create_event_post_type() {
register_post_type('event', [
'labels' => [
'name' => __('Events'),
'singular_name' => __('Event')
],
'public' => true,
'has_archive' => false,
'supports' => ['title', 'editor', 'custom-fields']
]);
}
add_action('init', 'create_event_post_type');Sample Program
This code registers a 'testimonial' post type. It will appear in the WordPress admin menu and supports title and content editing.
Wordpress
<?php
function create_testimonial_post_type() {
register_post_type('testimonial', [
'labels' => [
'name' => __('Testimonials'),
'singular_name' => __('Testimonial')
],
'public' => true,
'has_archive' => true,
'rewrite' => ['slug' => 'testimonials'],
'supports' => ['title', 'editor']
]);
}
add_action('init', 'create_testimonial_post_type');OutputSuccess
Important Notes
Always use unique slugs to avoid conflicts with existing post types.
Use the 'supports' array to control what editing features your post type has.
Flush rewrite rules by visiting Settings > Permalinks after registering a new post type to avoid 404 errors.
Summary
Custom post types help organize different content on your site.
Use register_post_type inside an init hook.
Set labels, visibility, and supported features to customize your post type.