Boilerplate to Create a WordPress Custom Post Types

Taimoor Sattar

We can use WordPress to hold and display different types of content. Different post_types have their own function and responsibility. 'post' is a post_type in WordPress, used to create the blog post for WordPress website but we can change its functionality depend on our website. WordPress also has the reserved post type:

  • post
  • page
  • attachment
  • revision
  • nav_menu_item
  • custom_css
  • customize_changeset
Alsoside with the reserved post_type we can also create custom post_type like film, Testimonial, games etc.

Creating Custom Post Type

1. Creating a basic Custom Post Type

In Wordpress, function register_post_type is used to create and modify a post type. To create and modify post type, You must call this function in the 'init' action.

function my_custom_posttypes() {
    $args = array(
      'public' => true,
      'label'  => 'Film'
    register_post_type( 'film', $args );
add_action( 'init', 'my_custom_posttypes' );
register_post_type is used here for creating custom post type named 'film'.

2. Building out an advanced Custom Post Type

To get more control on creating custom post type we have to enable more option.

function my_custom_posttypes() {
    $labels = array(
        'name'               => 'Films',
        'singular_name'      => 'Film',
        'menu_name'          => 'Films',
        'name_admin_bar'     => 'Film',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Film',
        'new_item'           => 'New Film',
        'edit_item'          => 'Edit Film',
        'view_item'          => 'View Film',
        'all_items'          => 'All Films',
        'search_items'       => 'Search Films',
        'parent_item_colon'  => 'Parent Films:',
        'not_found'          => 'No films found.',
        'not_found_in_trash' => 'No films found in Trash.',

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'menu_icon'          => 'dashicons-id-alt',
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'films' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => 5,
        'supports'           => array( 'title', 'editor', 'thumbnail' )
    register_post_type( 'film', $args );
add_action( 'init', 'my_custom_posttypes' );

Visit WordPress official doc, to get info about more technical term in register_post_type function.