Add a custom post type to WordPress with code

This code snippet allows you to add a custom post type to WordPress. In this snippet, we are adding a new CPT called "Custom Post Type" with the slug "custom_post_type".

PHP

function create_custom_post_type() {
  register_post_type( 'custom_post_type',
    array(
       'labels' => array(
        'name' => __( 'Custom Post Type' ),
        'singular_name' => __( 'Custom Post Type' )
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array( 'slug' => 'custom-post-type' ),
    )
  );
}
add_action( 'init', 'create_custom_post_type' );

Snippet Feedback

Did this snippet work for you? Do you have any questions about this snippet? Leave some feedback below.

Did it work?

Save Snippet

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.