How to register multiple custom post types in Wordpress

September 6, 2012 : Posted in: ,

How to register multiple custom post types - featured image

In this example we will show you how to register 2 custom post types(books and cars) as well as set the page editor options. To start, you’ll need to add the code below to your ‘functions.php’ file.

This is how you ‘register’ Custom Post Types(CPT) in your Wordpress installation.

function codex_custom_init() {

  register_post_type(
    'testimonials', array(
      'labels' => array('name' => __( 'Books' ), 'singular_name' => __( 'Book' ) ),
      'public' => true,
      'has_archive' => true,
      'supports' => array('title', 'editor', 'thumbnail')
    )
  );

  register_post_type(
    'home-messages', array(
      'labels' => array('name' => __( 'Cars' ), 'singular_name' => __( 'Car' ) ),
      'public' => true,
      'has_archive' => true,
      'supports' => array('title', 'editor', 'thumbnail')
    )
  );

}
add_action( 'init', 'codex_custom_init' );

NOTES:

  • Make a back up of your functions.php file before you begin!
  • Make sure you don’t already have custom post types registered or make a point to keep them separate or combined if so.
  • To add more CPTs simply add another register_post_type() function below the last one – after the ); .
  • Adding the ‘supports’ line allows you to control which editing options are available, i.e. featured image, excerpt, etc.

After saving you should now see your custom post types listed in the left column of the Dashboard. There are many variations to the CPT registration/features. More info can be found here: codex.wordpress.org/Function_Reference/register_post_type