How to create custom post types in WordPress


When WordPress was first introduced, it is used as blogging. But nowadays, it is used for building many complicating websites. It is the most widely used CMS nowadays. There are lots of options provided in the WordPress which can use to create any type of static as well as dynamic websites. WordPress comes with post and pages to contain the content. You can also create any number of posts and named them whatever you want. These post types are known as custom post types. In this article, we will show that how we can create custom post types in WordPress.

What is Custom Post type in WordPress?

Basically, custom post types are like normal pages and posts which are used to contain content. Custom post types are used to manage your content in a more structured way. You can attach categories, tags, and authors to your custom post. By default, WordPress comes with post types like pages, post, attachment, revision, Nav menu. Here we will show how o create a custom post type in WordPress.

We can create a custom post by using two methods:

1. Using Plugin
2. Using code

1. Using Plugin

If you are a beginner, then this is the best way to create a custom post. Also, the plugin comes with a number of options which you can easily customize.

First, you need to install a plugin named Custom Post Type UI. To know How to install and activate plugin in WordPress read our previous article.

Now go to CPT UI >> Add/Edit Post Types to create a custom post type.

There are a number of options provided which you can use. Fill the details as per your need and click on Add Post Type button.

You can also add custom taxonomies using this plugin. To add new taxonomies click on Add/Edit Taxonomies. Fill the details as per your need and click on Add Taxonomy button.

Also, you can customize many options as per your need. In this fashion, you can simply add custom post types easily.

2. Using code

Using plugin is not much effective because if you deactivate or delete the plugin, your all information lost. The best way to create a custom post is to add some code to your function.php file. This is the best way because all of your information is saved till you not change your theme. But for that, you should have knowledge of coding. You can add custom post type by adding few codes in function.php file.

Paste following code into your functions.php file:

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'Movies', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentythirteen' ),
        'all_items'           => __( 'All Movies', 'twentythirteen' ),
        'view_item'           => __( 'View Movie', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New Movie', 'twentythirteen' ),
        'add_new'             => __( 'Add New', 'twentythirteen' ),
        'edit_item'           => __( 'Edit Movie', 'twentythirteen' ),
        'update_item'         => __( 'Update Movie', 'twentythirteen' ),
        'search_items'        => __( 'Search Movie', 'twentythirteen' ),
        'not_found'           => __( 'Not Found', 'twentythirteen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentythirteen' ),
        'description'         => __( 'Movie news and reviews', 'twentythirteen' ),
        '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( 'latest' ),
        /* 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. 
*/
 

This code register a post type with a name ‘movies’. Also, we added other options like public visibility, has an archive, and slug that will be used in URLs for this post type, revisions and featured image.

We also added a taxonomy named ‘latest’.

Visit your dashboard and you can see Movies option is added to left side navigation.

Click on Add New to add a new post. This post is attached to Movies post type only.

In this way, you can add your own Custom Post Types in WordPress. We hope you like this article. If you have any query, you can ask in below comment box.

You can also check how we can add PHP code to the post.

Spread the love

Kapil Garg

Kapil Garg has 7 years of experience in building professional websites. He is a leading web developer, web designer and Google adwords certified partner. His passion is to designed and developed most impressive websites.

Leave a Reply

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