Need to add a custom post type to your WordPress website for things like Events, Portfolios, Products, Movies etc? Here’s a nice wee template/boilerplate/code snippet that will let you add a custom post type to WordPress in seconds. This is unfinished but published due to my own needs for this custom post type template :).

Copy and paste the code snippet below into your functions.php file, and edit the Events related text to match your new custom post type. I have included a simple reference to what each property refers to below the code snippet. Enjoy!

function add_event_cpt() {
  $labels = array(
    'name'                  => _x( 'Events', 'Singular name' ),
    'singular_name'         => _x( 'Event', 'Plural name' ),
    'add_new'               => _x( 'Add New', 'event' ),
    'add_new_item'          => __( 'Add New Event' ),
    'edit_item'             => __( 'Edit Event' ),
    'new_item'              => __( 'New Event' ),
    'all_items'             => __( 'All Events' ),
    'view_item'             => __( 'View Event' ),
    'search_items'          => __( 'Search Events' ),
    'not_found'             => __( 'No events found' ),
    'not_found_in_trash'    => __( 'No events found in the Trash' ), 
    'parent_item_colon'     => ’,
    'menu_name'             => 'Events'
  );
  $args = array(
    'labels'                => $labels,
    'description'           => 'The post type for our company events',
    'public'                => true,
    'publicly_queryable'    => true,
    'exclude_from_search'   => false,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'menu_icon'             => 'dashicons-calendar-alt',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'supports'              => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'           => true,
    'rewrite'               => array('slug' => 'events'),
    'hierarchical'          => true,
    'capability_type'       => 'post',
  );
  register_post_type( 'event', $args ); 
}
add_action( 'init', 'add_event_cpt' );

Basic Options

Pretty much every custom post type you register with WordPress needs these properties to be included for base level control. Further down this page you’ll find more advanced control, should you need it!

labels

description

public

menu_icon

supports

has_archive

rewrite

hierarchical

capability_type

Advanced Options

If you require more control over the specifics, here are some optional properties to really take your custom post type to the next level.

publicly_queryable

exclude_from_search

show_ui

show_in_menu

menu_position

show_in_admin_bar

show_in_nav_menus