Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I programmatically create a custom post type in WordPress?
Asked on Mar 11, 2026
Answer
Creating a custom post type in WordPress can be achieved by using the `register_post_type` function, typically within your theme's `functions.php` file or a custom plugin. This function allows you to define new post types with specific capabilities and settings.
<!-- BEGIN COPY / PASTE -->
function create_custom_post_type() {
$args = array(
'label' => 'Books',
'public' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'has_archive' => true,
'rewrite' => array('slug' => 'books'),
);
register_post_type('book', $args);
}
add_action('init', 'create_custom_post_type');
<!-- END COPY / PASTE -->Additional Comment:
- The `register_post_type` function should be hooked to the 'init' action to ensure it runs at the correct time.
- Customize the `$args` array to include additional features like custom taxonomies or different capabilities.
- Remember to flush rewrite rules by visiting the "Settings → Permalinks" page after registering a new post type.
Recommended Links:
