Create custom post type in wordpress

Wordpress Tutorials, WrodPress Programmatically

Create custom post type in wordpress

Custom post type in wordpress

WordPress can manage many types of contents and it’s robust CMS. WordPress comes with different types of default post type like the post is a specific post type. All post types manage in a wp_posts table in a database. below is the code for how to create custom post type in WordPress.

WordPress comes with following default post types

  • Page
  • Post
  • Attachment
  • Revision
  • Navigation Menu
  • Changesets
  • Custom CSS

 

What is WordPress custom post types

Custom post type is contents like pages and posts. We can create custom post type similar like this so below is the code for how to create custom post type in WordPress. By using register_post_type ($post_type, $args) function we can create new custom post type.

 

$post_type (required) : Name of the post type

$args (optional): An array of arguments

 

Create a custom post types manually in WordPress

For creating a custom post type manually you add the following code in a functions.php file on your theme.

function my_custom_post_type() {

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

   // Set other options for Custom Post Type

    $args = array(
        'label'               => __( 'movies', 'textdomain' ),
        'description'         => __( 'Movie news and reviews', 'textdomain' ),
        '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( 'movies', $args );

}

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

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

Then after custom post type Movies created when you see on the WordPress admin dashboard and below is the screenshot for admin side display menu.

 

custom post type menu

 

Arguments

label (string)(optional): An array of labels for this custom post type.

description (string)(optional): A short description for this custom post type.

public (boolean)(optional): Controls for how the type is visible to authors and readers.

exclude_from_search (boolean)(importance): Whether to exclude posts with this post type from front end search results.

publicly_queryable (boolean)(optional)Whether queries can be performed on the front end as part of the  parse_request() function.

show_ui (boolean)(optional): Whether to generate a default UI for managing this post type in the admin.

show_in_nav_menus (boolean)(optional): Where to show the post type in the admin menu. show_ui must be true.

show_in_admin_bar (boolean)(optional): Whether to make this post type available in the WordPress admin bar.

menu_position (integer)(optional): The position in the menu order the post type should appear. show_in_menu must be true.

menu_icon (string)(optional): The URL path to the icon to be used for this menu.

capability_type (string or array)(optional): The string to use to build the read, edit, and delete capabilities.

capabilities (array)(optional): An array of the capabilities for this post type.

map_meta_cap (boolean)(optional): Whether to use the internal default meta capability handling.

hierarchical (boolean)(optional): Whether the post type is hierarchical (e.g. page).

supports (array/boolean)(optional): An alias for calling add_post_type_support() directly.

register_meta_box_cb (callback)(optional): Provide a callback function that will be called when setting up the meta boxes for the edit form.

taxonomies (array)(optional): An array of registered taxonomies like category or that post_tag will be used with this post type.

has_archive (boolean or string)(optional): Triggers the handling of rewrites for this post type. To prevent rewrites, set to false.

permalink_epmask (array)(optional): The default rewrite endpoint bitmasks.

query_var (boolean or string)(optional): Sets the query_var key for this post type.

can_export (boolean)(optional): Can this post_type be exported.

delete_with_user (boolean)(optional): Whether to delete posts of this type when deleting a user. If true, posts of this type belonging to the user will be moved to trash when then a user is deleted. If false, posts of this type belonging to the user will not be trashed or deleted.

show_in_rest (boolean)(optional): Whether to expose this post type in the REST API.

rest_base (string)(optional): The base slug that this post type will use when accessed using the REST API.

rest_controller_class (string)(optional): An optional custom controller to use instead of WP_REST_Posts_Controller.

8 thoughts on “Create custom post type in wordpress

  1. Very good blog! Do you have any tips and hints for aspiring writers? I’m hoping to start my own site soon but I’m a little lost on everything. Would you advise starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m totally confused .. Any tips? Thanks!

  2. After I initially commented I clicked the -Notify me when new comments are added- checkbox and now each time a remark is added I get four emails with the identical comment. Is there any method you’ll be able to remove me from that service? Thanks!

Leave a Reply