Registering Custom Post Type in WordPress

Capability of registering a Custom Post Type (CPT) in WordPress is one of the most important features that WP has ever added on it's core files. It has paved the way for many WP enthusiasts and developers to think WP not only as a Blogging platform but as a full fledged CMS.

Using WordPress's "register_post_type" function you can easily create your own custom post type. Today in this post I will show you how to register a Custom Post Type (CPT) in WordPress. This is fairly easy and wouldn't take long for anyone to understand.

For demonstration purpose, let's assume that you want to register a post type called "Books" and under this CPT you would write various book reviews that you have read. This can be done simply copying and pasting the following snippet on your theme's functions.php file.

function register_books_post_type() {
   register_post_type('books', array(
      'labels' => array(
         'name' => 'Books',
         'singular_name' => 'Book',
         'add_new' => 'Add Review',
         'add_new_item' => 'Add New Review',
         'edit_item' => 'Edit Review',
         'new_item' => 'New Review',
         'view_item' => 'View Review',
         'all_items' => 'All Review',
         'search_items' => 'Search Review',
         'not_found' =>  'No Review Found',
         'not_found_in_trash' => 'No Review in the trash',
         'parent_item_colon' => '',
         'menu_name' => 'Books'),
      'public' => true,
      'publicly_queryable' => true,
      'show_ui' => true,
      'show_in_menu' => true, 
      'exclude_from_search' => false,
      'query_var' => true,
      'capability_type' => 'post',
      'rewrite' => array('slug' => 'books'),
      'can_export' => true,
      'has_archive' => books,
      'hierarchical' => 'false',
      'menu_position' => 50,
      'menu_icon' => get_template_directory_uri() . '/images/icon.png',
      'supports' => array('title','editor','thumbnail','comments'))); 

This is pretty much everything you would need to get started. However, there are so many ways to play around with this snippet depending on your own needs. Most of the Parameters and arguments can be customized. Simply visit the WordPress Codex page for register_post_type function and adjust the parameters anyway you want.

If everything goes smoothly, your custom book reviews post's url should be:

Feel free to try it out on your local server first before you implement it on your actual site (Even though I already tested it on my local server), just to be on safe side. Also, if you have any question, feel free to post it from the comments section below. Hope that helps.


Commenting is disabled.