How to Create Custom Post Type in WordPress

How to Create Custom Post Type in WordPress

Custom post types are same like posts and pages. By default WordPress comes with a few different post types which are all stored in the database under the wp_posts table.

Default Post Types

  • Posts
  • Pages
  • Attachments
  • Revisions
  • Navigation Menus
  • Custom CSS
  • Changesets

We can create your own custom post types and call them whatever we want. On below example we register a post type ‘events’ with an array of arguments. These arguments are the options of our custom post type. Code goes in function.php file of your active child theme (or active theme).

<?php 
function create_movies_post_type() {

    /* Set UI labels for Movies Post Type */

    $labels = array(
        'name'                => _x( 'Movies', 'Post Type Normal Name', 'twentytwenty' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwenty' ),
        'menu_name'           => __( 'Movies', 'twentytwenty' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwenty' ),
        'all_items'           => __( 'All Movies', 'twentytwenty' ),
        'view_item'           => __( 'View Movie', 'twentytwenty' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwenty' ),
        'add_new'             => __( 'Add New', 'twentytwenty' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwenty' ),
        'update_item'         => __( 'Update Movie', 'twentytwenty' ),
        'search_items'        => __( 'Search Movie', 'twentytwenty' ),
        'not_found'           => __( 'Not Found', 'twentytwenty' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwenty' ),
    );

    /* Other arguments for Movies Post Typ- */

    $args = array(
        'label'               => __( 'movies', 'twentytwenty' ),
        'description'         => __( 'Movie news and reviews', 'twentytwenty' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        'taxonomies'          => array( 'genres' ),
        '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'     => 'post',
        'show_in_rest' => true,
    );

    /* Registering your Movies Post Type */
    register_post_type( 'movies', $args );

}

add_action( 'init', 'create_movies_post_type', 0 );
Was this article helpful?
YesNo

1 comment

  1. Wow, marvelous blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site is
    magnificent, as well as the content!

Leave a comment

Your email address will not be published. Required fields are marked *