Blog

How to Create Custom Post Type Manually In WordPress?

WordPress is often mistaken as ‘just’ a blogging platform. Over years, WordPress has evolved into a dynamic content management system. Post and pages are included into WordPress as main content types by default. However, users can create any number of custom content. These are referred to as ‘Custom Post Types’.

How to Create Custom Post Type Manually In WordPress

Here, you will get to know an easy and very effective method to develop custom post types in WordPress.

Understanding WordPress Custom Post Type

These are content types similar to simple posts and pages. WordPress has originally evolved from a simple blogging platform into a dynamic CMS. But the term ‘post’ got attached to it. However, a post type can be any kind of content.

WordPress is available with the following post types (by default):

– Post

– Attachment

– Page

– Revision

– Nav Menu

You can develop your own custom post types. These can also be named as you like.

For instance, if you run a website for cosmetics review, then you would like to create a post type based on reviews for various cosmetics. You can include varied custom fields to this post type as required and its own custom category structure. Some of the examples of common post types include:

– Portfolio
– Testimonials
– Products

Creating a Custom Post Type

  1. The Easy Method

Using a plugin is the easiest way to create a custom post type in WordPress. Experts use this method for beginners as it is very safe and easy.

Step 1: Install and activate the Custom Post Type UI plugin.
Step 2: Upon activation, the plugin will add a new menu item in WordPress admin menu. This item is referred to as ‘CPT UI’.
Step 3: Go to CPT UI and ‘Add New’ to create a new custom post type.

The UI plugin for Custom Post Type allows users to create custom taxonomies. No wonder ‘Add new custom post type’ page is categorized into two columns.

The left side has the form you require filling in for creating your custom post type. The right side of page has a form to create a custom taxonomy (if needed).

Give a name for custom post type in the column. Make sure the name does not exceed over 20 characters. For instance, you can add cosmetics, skin types, deal, glossary, etc.

Adding New Custom Post Type

Step 1: You should provide a label in the next field for your custom post type. The label will appear in your WordPress admin bar which is quite similar to posts and pages. Make sure it is plural and makes sense. For instance, you can add e.g. Cosmetics, Skin Types, Deals, Glossary, etc.

Step 2: Once this is done, you require providing a singular form for your label. The form will be used by WordPress for displaying instructions and various other user interface elements.

Step 3: Enter a description for your custom post type. The description is used to elaborate the duty of your post type.

Step 4: Click on the ‘Create Custom Post Type’ button. This will add your new custom post type.

You can also click on the Advanced Label Options and Advanced Options links to customize more options for your custom post type.

How to Create a Custom Post Type Manually

Using a plugin is breeze. However, it has one disadvantage. When you use a plugin, the custom post types will disappear as soon as the plugin gets deactivated. Although data in those custom post types will be still available, your custom post type will be unregistered. You cannot access it from the admin area.

So if you are working on a client site, and do not wish to install another plugin, creating custom post type manually works the best. This is done by adding the required code in the ‘functions.php file’ of your theme or in a site-specific plugin.

How to Do This?

Have a close look at this code:

// Our custom post type function

function create_posttype() {

register_post_type( ‘cosmetics’,

// CPT Options

array(

‘labels’ => array(

‘name’ => __( ‘Cosmetics’ ),

‘singular_name’ => __( ‘Cosmetics’ )

),

‘public’ => true,

‘has_archive’ => true,

‘rewrite’ => array(‘slug’ => ‘cosmetics’),

)

);

}

// Hooking up our function to theme setup

add_action( ‘init’, ‘create_posttype’ );

The above mentioned code registers a post type ‘cosmetics’ with a series of arguments. These arguments are options of custom post type. The array comprises of two parts:

First Part – Labels (an array)

Second Part – Arguments such as public visibility, archive, and slug. These will be used in URLs for this post type.

Here is a detailed view of the code that adds additional options to your custom post type.

/*

* Creating a function to create our CPT

*/

 

function custom_post_type() {

// Set UI labels for Custom Post Type

$labels = array(

‘name’                => _x( ‘Cosmetics’, ‘Post Type General Name’, ‘BobbyBrown’ ),

‘singular_name’       => _x( ‘Cosmetics’, ‘Post Type Singular Name’, ‘BobbyBrown’ ),

‘menu_name’           => __( ‘Cosmetics’, ‘BobbyBrown’ ),

‘parent_item_colon’   => __( ‘Parent Cosmetics’, ‘BobbyBrown’ ),

‘all_items’           => __( ‘All Cosmetics’, ‘BobbyBrown’ ),

‘view_item’           => __( ‘View Cosmetics’, ‘BobbyBrown’ ),

‘add_new_item’        => __( ‘Add New Cosmetics’, ‘BobbyBrown’ ),

‘add_new’             => __( ‘Add New’, ‘BobbyBrown’ ),

‘edit_item’           => __( ‘Edit Cosmetics’, ‘BobbyBrown’ ),

‘update_item’         => __( ‘Update Cosmetics’, ‘Bobbybrown’ ),

‘search_items’        => __( ‘Search Cosmetics’, ‘BobbyBrown’ ),

‘not_found’           => __( ‘Not Found’, ‘BobbyBrown’ ),

‘not_found_in_trash’  => __( ‘Not found in Trash’, ‘BobbyBrown’ ),

);

// Set other options for Custom Post Type

$args = array(

‘label’               => __( ‘cosmetics’, ‘BobbyBrown’ ),

‘description’         => __( ‘Cosmetics news and reviews’, ‘BobbyBrown’ ),

‘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( ‘cosmetics’, $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’, 0 );

In the above example, many more options have been added to the custom post type with this code. This allows adding of many more features such as support for revisions, featured image, custom fields, and many more. If you don’t see your question here, please feel free to ask us: jiteshmanaktala.com/about/

This custom post type has also been related to a custom taxonomy referred to as genres. The part where the hierarchical value has been set to be false should also be observed carefully. In case, you wish that your custom post type behave like Pages, it is important to set this value to true.

There is repeated use of BobbyBrown. This is known as text domain. In case, your theme is translation ready and you wish your custom post types to get translated, then you will require mentioning text domain used by your theme.

The style.css file in theme directory will have text domain of your theme. Text domain is mentioned in the header of the file.

Leave a Reply

Your email address will not be published.