How to create custom post type in WordPress ?

There are several post types that are readily available to users or internally used by the WordPress installation by default :

  • Post (Post Type: ‘post’)
  • Page (Post Type: ‘page’)
  • Attachment (Post Type: ‘attachment’)
  • Revision (Post Type: ‘revision’)
  • Navigation Menu (Post Type: ‘nav_menu_item’)
  • Custom CSS (Post Type: ‘custom_css’)
  • Changesets (Post Type: ‘customize_changeset’)
  • User Data Request (Post Type: ‘user_request’ )

Custom Post Types

Custom post types are new post types you can create. A custom post type can be added to WordPress via the register_post_type() function. This function allows you to define a new post type by its labels, supported features, availability and other specifics.

Note that you must call register_post_type() before the admin_menu and after the after_setup_theme action hooks. A good hook to use is the init hook.

Here’s a basic example of adding a custom post type:

function create_post_type() {
	register_post_type( 
		'century_theme',     
		array(
			'labels' => array(
				'name' => esc_html__( 'Century Themes', 'text-domain' ),
				'singular_name' => esc_html__( 'Century Theme', 'text-domain' )
			),
			'public' => true,
			'has_archive' => true,
		)   
	); 
} 
add_action( 'init', 'create_post_type' );