Using WordPress ‘register_post_type()’ PHP function

The register_post_type() WordPress PHP function registers a custom post type in your WordPress installation.

Usage

register_post_type( $post_type, $args );

Example:

Input:

register_post_type( 'book', array(
  'labels' => array(
    'name' => __( 'Books' ),
    'singular_name' => __( 'Book' ),
  ),
  'public' => true,
  'has_archive' => true,
));

Output: A custom post type ‘Book’ with plural label ‘Books’ and archive support.

Parameters

  • $post_type (string) (required): The post type key (maximum 20 characters). Must contain only lowercase alphanumeric characters, dashes, and underscores.
  • $args (array|string) (optional): An array or string of arguments for registering the post type.

More information

See WordPress Developer Resources: register_post_type()

Examples

Register a basic custom post type ‘Movie’

This example registers a custom post type called ‘Movie’ with plural label ‘Movies’ and public visibility.

function create_movie_post_type() {
  register_post_type( 'movie',
    array(
      'labels' => array(
        'name' => __( 'Movies' ),
        'singular_name' => __( 'Movie' )
      ),
      'public' => true,
    )
  );
}
add_action( 'init', 'create_movie_post_type' );

Register a custom post type ‘Event’ with custom arguments

This example registers a custom post type ‘Event’ with custom arguments, including a custom menu icon.

function create_event_post_type() {
  register_post_type( 'event',
    array(
      'labels' => array(
        'name' => __( 'Events' ),
        'singular_name' => __( 'Event' ),
      ),
      'public' => true,
      'menu_icon' => 'dashicons-calendar-alt',
    )
  );
}
add_action( 'init', 'create_event_post_type' );

Register a hierarchical custom post type ‘Guide’

This example registers a hierarchical custom post type ‘Guide’, which behaves similarly to the default ‘Page’ post type.

function create_guide_post_type() {
  register_post_type( 'guide',
    array(
      'labels' => array(
        'name' => __( 'Guides' ),
        'singular_name' => __( 'Guide' ),
      ),
      'public' => true,
      'hierarchical' => true,
    )
  );
}
add_action( 'init', 'create_guide_post_type' );

Register a custom post type ‘Product’ with custom taxonomy ‘Category’

This example registers a custom post type ‘Product’ with a custom taxonomy ‘Category’.

function create_product_post_type() {
  register_post_type( 'product',
    array(
      'labels' => array(
        'name' => __( 'Products' ),
        'singular_name' => __( 'Product' ),
      ),
      'public' => true,
      'taxonomies' => array( 'category' ),
    )
  );
}
add_action( 'init', 'create_product_post_type' );

Register a custom post type ‘Portfolio’ with support for post thumbnails and custom fields

This example registers a custom post type ‘Portfolio’ with support for post thumbnails and custom fields.

function create_portfolio_post_type() {
  register_post_type( 'portfolio',
    array(
      'labels' => array(
        'name'