Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How do I programmatically create a custom post type with WordPress?
Asked on Jan 13, 2026
Answer
Creating a custom post type in WordPress programmatically involves using the `register_post_type` function within your theme's `functions.php` file or a custom plugin. This allows you to define new content types beyond the default posts and pages.
<!-- BEGIN COPY / PASTE -->
function my_custom_post_type() {
$args = array(
'labels' => array(
'name' => 'Books',
'singular_name' => 'Book'
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'books'),
'supports' => array('title', 'editor', 'thumbnail'),
);
register_post_type('book', $args);
}
add_action('init', 'my_custom_post_type');
<!-- END COPY / PASTE -->Additional Comment:
- Place the code in your theme's `functions.php` file or a custom plugin to ensure it runs on WordPress initialization.
- Customize the `$args` array to fit the needs of your custom post type, such as adding support for custom fields or taxonomies.
- Always test new code in a staging environment before deploying to a live site.
Recommended Links:
