Creating a New Post Type in WordPress

create-your-own-post-type-in-wordpressManaging a new type of post can be an intimidating process..

Give this version with concise and proper syntax a try!

This is my favorite method that I use when creating a new post type.  I’ll sometimes requires some special CSS or JS to accomodate some special features.  The most important part is properly addressing the labels and search functions of the post types themselves.

In this example we are creating a new post type called ‘course_materials’

// lets create a new post type called 'course_material'
// Set UI labels for Custom Post Type 
// we'll use that post type and reference it like a page 
function make_course_material_post_type() { 
     $labels = array(
          'name' => 'Course Materials',          // Post Type General Name
          'singular_name' => 'Course Material',  // Post Type Singular Name
          'menu_name' => 'Materials', 
          'parent_item_colon' => 'Parent Material',
          'all_items' => 'All Course Materials',
          'view_item' => 'View Course Material',
          'add_new_item' => 'Add New Course Material',
          'add_new' => 'Add New',
          'edit_item' => 'Edit Course Material',
          'update_item' => 'Update Course Material',
          'search_items' => 'Search Materials', 
          'not_found' => 'No Course Materials Found', 
          'not_found_in_trash' => 'No Course Materials Found in Trash',
      );
      // Set main options for Custom Post Type
      $args = array( 
          'label' => 'course_materials',
          'description' => 'Course Materials for Student Registrants',
          'labels' => $labels,

          // Features this CPT supports in Post Editor
          'supports' => array( 'title', 'editor',
                               'author', 'revisions', 
                               // other options include...
                               // 'thumbnail','custom-fields',
                               // 'excerpt','comments',*/
          ),

          // You can associate this CPT with a taxonomy or custom taxonomy. 
          // 'taxonomies' => array( 'genres' ),
          // A hierarchical CPT is like Pages and can have
          // Parent and child items. A non-hierarchical CPT
          // is like Posts.

         'hierarchical' => false,
         'public' => true,
         'show_ui' => true,
         'show_in_menu' => true,
         'show_in_nav_menus' => true,
         'show_in_admin_bar' => true,
         'menu_position' => 20,
         'can_export' => true,
         'has_archive' => true,
         'exclude_from_search' => true,          // hide from searches!
         'publicly_queryable' => true,
         'capability_type' => 'page',            // add an admin icon here
         'menu_icon' => get_template_directory_uri() . '/images/admin-course-materials-icon.png',
     );

     // Registering your Custom Post Type!
     register_post_type( 'course_materials', $args ); 
 }

// Hook into the 'init' action so that the function
// Containing our post type registration is not 
// unnecessarily executed. 

add_action( 'init', 'make_course_material_post_type', 0 );

Leave a Reply