How to Create Custom Post Types in WordPress

By default, WordPress allows you to publish two types of articles: Posts and Pages. When you write a typical article, you usually publish it as a “post”. When you publish contents about your website — like your website’s “About Us” or “Contact Us” page — you publish it as a “page”. If you use WordPress for a blog, it is the Posts that will be shown in your articles list, not the Pages. But what if you want another type of content for your website? You will need to create a custom post type.

Open the functions.php file of your WordPress website and paste the following code in it — make sure you edit it to suit your needs:

/*
* Create a Custom Post Type (CPT)
*/
 
function custom_post_type_tutorials() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Tutorials', 'Post Type General Name' ),
        'singular_name'       => _x( 'Tutorial', 'Post Type Singular Name' ),
        'menu_name'           => __( 'Tutorials' ),
        'parent_item_colon'   => __( 'Parent Tutorial' ),
        'all_items'           => __( 'All Tutorials' ),
        'view_item'           => __( 'View Tutorial' ),
        'add_new_item'        => __( 'Add New Tutorial' ),
        'add_new'             => __( 'Add New' ),
        'edit_item'           => __( 'Edit Tutorial' ),
        'update_item'         => __( 'Update Tutorial' ),
        'search_items'        => __( 'Search Tutorial' ),
        'not_found'           => __( 'Not Found' ),
        'not_found_in_trash'  => __( 'Not found in Trash' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'tutorials' ),
        'description'         => __( 'Tutorial news and reviews' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // 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'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );
     
    // Registering your Custom Post Type
    register_post_type( 'tutorials', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type_tutorials', 0 );