How to Create Custom Post Types in WordPress

WordPress is a powerful platform offering lots of powerful features that is more than blogging features.Today i will discuss one of the top feature of wordpress called Custom Post Types.Most of us classify wordpress only as a blogging platform and don’t know about their features.Few days ago i had discussed about How to create custom taxonomy in wordpress and today i will discuss about how to create Custom post types in wordpress.
Custom Post Types in WordPress
So first of all let’s know that what is Custom post type?

What is Custom Post Types?

Custom post types are content types like posts and pages.Since WordPress evolved from a simple blogging platform into a robust CMS, the term post stuck to it. Since the release of WordPress 3.0, you can now create your own custom post types.Here are some post types which comes with WordPress:

  1. Post (Post Type: post)
  2. Page (Post Type: page)
  3. Attachment (Post Type: attachment)
  4. Revision (Post Type: revision)
  5. Navigation menu (Post Type: nav_menu_item)

You can create your own custom post type and call them whatever you want.
For Example: I had created a Affiliate Network Review Post type and i needed custom post type,custom taxonomy,custom fields.
Another examples of Custom post types are Product, Portfolio, Testonomials.

Create Custom Post Types

We can create it by two methods that is By using plugin or by self coding.
If you are not an programmer or you don’t know much about coding then you can use Plugin.

1.Create Custom Post Types using Plugin

We can use Custom Post Type UI plugin to create custom post types and custom taxonomies.
We can create both Custom Post types and custom taxonomies using this plugin.
My Coding Tricks Here are few steps which will help you in creating custom post types:
Step 1: Install Custom Post Type UI plugin and Activate it.
Step 2: Now go to CPT UI>>Add New.
Add New CPT UI
Step 3: Fill the Details of your Custom post type
Create Custom Post Types
If you want to label You custom Post type then click on the Advance Label Options
Advance Label Options for Custom Post Types
Advance Label Options for Custom Post Types 2
If you wants to edit some Advance options then you can edit them.Click on the Advanced Options.
Advance Options for Custom Post types
Advance Options for Custom Post types 2
Advance Options for Custom Post types 3
You can go to http://codex.wordpress.org/Function_Reference/register_post_type for further details of advance options and if you are not getting anything then ask me through comments.

2. Create Custom Post Type Manually

The problem with using plugin is that when you deactivate the plugin you will be not able to access your custom post types from your Admin area.Your data will be there but you can’t access it from your Admin Area.
If you don’t want to install plugin then you can create custom post types manually.
Step 1: Go to Appearance>>Editor
Go to WordPress Theme Editor
Step 2: Now go to functions.php file of your theme.
Go to function file of your theme
Step 3:Paste the Code which you like.
Here is the simple and short code and its fully working.This is the Basic code.

function add_new_post_type() {
    $args = array(
      'public' => true,
      'label'  => 'Affiliate Network'
    );
    register_post_type( 'affreview', $args );
}
add_action( 'init', 'add_new_post_type' );

Here is the elaborated code.

add_action('init', 'add_new_post_type');
function add_new_post_type() {
    register_post_type('affreview', array(
        'label' => 'Affiliate Networks Reviews',
        'description' => 'CPA network reviews, network details, cpa offers, affiliate marketer community, industry news and more at My Coding Tricks.',
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => true,
        'rewrite' => array('slug' => 'affreview', 'with_front' => true),
        'query_var' => true,
        'has_archive' => true,
        'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes','post-formats'),
        'taxonomies' => array('affiliatenetworks'),
        'labels' => array (
            'name' => 'Affiliate Networks Reviews',
            'singular_name' => 'Affiliate Network Review',
            'menu_name' => 'Affiliate Networks',
            'add_new' => 'Add Affiliate Network',
            'add_new_item' => 'Add New Affiliate Network',
            'edit' => 'Edit',
            'edit_item' => 'Edit Affiliate Network',
            'new_item' => 'New Affiliate Network',
            'view' => 'View Affiliate Network',
            'view_item' => 'View Affiliate Network',
            'search_items' => 'Search Affiliate Network',
            'not_found' => 'Affiliate Network Not Found',
            'not_found_in_trash' => 'No Affiliate Network found in Trash',
            'parent' => 'Parent Affiliate Network',
        )
    ) );
}

Custom Post type PHP Code
That’s All.

[moptin id="3098"]

Displaying Custom Post types to Front Page

To display the custom post type to front page use the following code.

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
	if ( is_home() && $query->is_main_query() )
		$query->set( 'post_type', array( 'post', 'affreview' ) );
	return $query;
}

Don’t forget to replace affreview to your custom post type name in all the above codes.
Here is the demo of Custom Post type.
Custom post type Demo

Note: Please do not forget to follow us and share us.If you have any doubt/question/recommendation then comment below.
Do not forget to subscribe us through email.

Shubham Kumar

Hey, I am Shubham and i love Blogging, Coding and exploring new things and obviously sharing my experience with you.

You may also like...

1 Response

  1. Avinash says:

    You had nicely explained the article.Too Good.
    Keep it up.

Leave a Reply

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