Here we are going to discuss on how to create custom post type in wordpress and will make a simple function to use.

What is Custom Post Type?

‘Custom post type’ is one of the great features of wordpress since v3.0. It allows you to add different custom post types for different purposes. By default wordpress uses post_type ‘post’ for looping posts you have written. But if want to have Books, Portfolio etc. then it would be easily manageable if you create relative custom post types. Reserved post types are: ‘post‘, ‘page‘, ‘revision‘, ‘attachment‘ and ‘nav_menu_item

Function to create Custom Post Type

Following function need to add in your theme’s functions.php file.

function my_custom_post_type() {
  register_post_type('book', array(
    'labels' => array(
      'name'               => 'Books',
      'singular_name'       => 'Books',
      'add_new'            => 'Add new Book',
      'edit_item'          => 'Edit Book',
      'new_item'           => 'New Book',
      'view_item'          => 'View Books',
      'search_items'       => 'Search Books',
      'not_found'          => 'No Book found',
      'not_found_in_trash' => 'No Book found in Trash'
    ),
    'menu_position' => 6,
    'menu_icon'    => get_template_directory_uri().'/images/book-icon.png',
    'public'       => true,
    'supports'     => array('title','editor','author','thumbnail','excerpt','trackbacks','comments','page-attributes', 'custom-fields', 'revisions'),
    'taxonomies'   => array('category', 'post_tag')
  ));
  flush_rewrite_rules();
}

and then add this function in wordpress action hook

add_action('init', 'my_custom_post_type');

Once you are done with above stuff you will find ‘Books’ custom post type tab in admin menu below ‘Posts’ tab. In above function I have added all the features support for custom post type ‘Book’. Thus it will have all the structure that ‘post’ has. If you do not want any of the supports, exclude that from the ‘supports’ list. Similarly here the default ‘category’ and post tags will be attached to this custom post type. If you do not required then just comment out ‘taxonomies’. You can also change the ‘menu_icon’ to point to your custom post type. Make sure that icon path is correct else it will not show any icon.

Now try adding some posts in newly created custom post type and view one post to see how it looks.

Generic function to create multiple post types

To make this process easy I have created one function that I use in my almost every wordpress project.

function nyCustomPostType($nycp_name, $nycp_slug, $show_category) {
  $show_category = $show_category == true ? $nycp_slug.'category' : '';
  register_post_type($nycp_slug, array(
    'labels' => array(
      'name'                => $nycp_name,
      'singular_name'       => $nycp_name,
      'add_new'             => 'Add new '.$nycp_name,
      'edit_item'           => 'Edit '.$nycp_name,
      'new_item'            => 'New '.$nycp_name,
      'view_item'           => 'View '.$nycp_name,
      'search_items'        => 'Search '.$nycp_name,
      'not_found'           => 'No '.$nycp_name.' found',
      'not_found_in_trash'  => 'No '.$nycp_name.' found in Trash'
    ),
    'menu_position' => 6,
    'menu_icon'     => get_template_directory_uri().'/images/icon.png',
    'public'        => true,
    'supports'      => array('title','editor','thumbnail','excerpt','page-attributes', 'custom-fields', 'revisions'),
    'taxonomies'    => array($show_category)
  ));
  flush_rewrite_rules();
}

function ny_custom_post_types() {
  /* USAGE: nyCustomPostType('POST TYPE NAME', 'post-type-slug', true/false); */
  nyCustomPostType('Books', 'book', false);
  // create another post type
  nyCustomPostType('My Portfolio', 'portfolio', false);
  // one more post type with category support
  nyCustomPostType('Pictures', 'picture', true);
}
add_action('init', 'ny_custom_post_types');

Please note that post slug should be unique. It does not already be used for pages or posts else your post type will not work properly.

What Next? Custom Post Type Navigation with/without numbered pagination

You might also like: