How to create custom post types on WordPress
Custom post types in WordPress can be created by adding code to your theme's functions.php file or using a plugin. You'll need to register the post type using the register_post_type() function with specific parameters.
Prerequisites
- WordPress admin access
- Basic PHP knowledge
- Text editor or code editor
- Active WordPress theme
Step-by-Step Instructions
Access your WordPress files
functions.php file. Alternatively, you can create a custom plugin file in the /wp-content/plugins/ directory.Add the custom post type registration code
functions.php file and add the following code before the closing PHP tag:function create_custom_post_type() {
register_post_type('your_post_type',
array(
'labels' => array(
'name' => 'Custom Posts',
'singular_name' => 'Custom Post',
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail'),
)
);
}
add_action('init', 'create_custom_post_type');Configure post type labels and settings
'labels' => array(
'name' => 'Products',
'singular_name' => 'Product',
'add_new' => 'Add New Product',
'add_new_item' => 'Add New Product',
'edit_item' => 'Edit Product',
'new_item' => 'New Product',
'view_item' => 'View Product',
'search_items' => 'Search Products',
'not_found' => 'No products found',
'not_found_in_trash' => 'No products found in trash'
),Set post type capabilities and features
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'),
'menu_icon' => 'dashicons-products',
'menu_position' => 5,Set
'show_in_rest' => true to enable Gutenberg editor support.Save and refresh permalinks
functions.php file and navigate to your WordPress admin dashboard. Go to Settings > Permalinks and click Save Changes without making any modifications. This flushes the rewrite rules and ensures your custom post type URLs work correctly.Verify the custom post type appears
Create template files for frontend display
single-your_post_type.phpfor individual post displayarchive-your_post_type.phpfor the archive page
Copy existing template files like
single.php or archive.php and modify them as needed.Common Issues & Troubleshooting
Custom post type doesn't appear in admin menu
Check that 'show_ui' => true and 'show_in_menu' => true are set in your registration code. Also verify there are no PHP syntax errors in your functions.php file.
404 errors when viewing custom post type pages
Go to Settings > Permalinks and click Save Changes to flush rewrite rules. Ensure 'public' => true and 'publicly_queryable' => true are set in your registration.
Custom post type posts don't show in frontend loops
By default, custom post types are excluded from main queries. Use pre_get_posts action or modify your queries to include custom post types using the 'post_type' parameter.
Gutenberg editor not working with custom post type
Add 'show_in_rest' => true to your post type registration arguments. This enables REST API support required for the Gutenberg editor.