Custom Post Types in WordPress
Learn how to create and register custom post types in WordPress. This snippet provides a clean, reusable function for adding custom post types to your theme or plugin.
Code Snippet
/**
* Register Custom Post Type
*
* @param string $post_type Post type name
* @param string $singular Singular name
* @param string $plural Plural name
* @param array $args Additional arguments
*/
function register_custom_post_type($post_type, $singular, $plural, $args = []) {
$labels = [
'name' => $plural,
'singular_name' => $singular,
'menu_name' => $plural,
'add_new' => 'Add New',
'add_new_item' => 'Add New ' . $singular,
'edit_item' => 'Edit ' . $singular,
'new_item' => 'New ' . $singular,
'view_item' => 'View ' . $singular,
'search_items' => 'Search ' . $plural,
'not_found' => 'No ' . strtolower($plural) . ' found',
'not_found_in_trash'=> 'No ' . strtolower($plural) . ' found in Trash'
];
$defaults = [
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'supports' => ['title', 'editor', 'thumbnail', 'excerpt']
];
$args = wp_parse_args($args, $defaults);
register_post_type($post_type, $args);
}
// Example usage:
register_custom_post_type(
'portfolio',
'Portfolio Item',
'Portfolio Items',
[
'menu_icon' => 'dashicons-portfolio',
'supports' => ['title', 'editor', 'thumbnail', 'excerpt', 'custom-fields']
]
);
How to Use
Add this code to your theme's functions.php
file or in your custom plugin. Then call the function with your desired post type details.
Example Usage
// Register a Portfolio post type
register_custom_post_type(
'portfolio',
'Portfolio Item',
'Portfolio Items',
[
'menu_icon' => 'dashicons-portfolio',
'supports' => ['title', 'editor', 'thumbnail', 'excerpt', 'custom-fields']
]
);
// Register a Testimonials post type
register_custom_post_type(
'testimonial',
'Testimonial',
'Testimonials',
[
'menu_icon' => 'dashicons-format-quote',
'supports' => ['title', 'editor', 'thumbnail']
]
);
Notes
- This function provides a clean, reusable way to register custom post types
- All arguments are optional and will fall back to sensible defaults
- Supports Gutenberg editor by default (show_in_rest => true)
- Includes common features like title, editor, thumbnail, and excerpt support