Beginner's Guide for Wordpress

How to create wordpress custom post types?


As you are here, I suppose you already know what custom post types are. So I would get straight to the business of creating one for you.
First open your theme folder. Now find the functions.php file and in it write the following code:

function my_custom_post_product(){

$args =array();
register_post_type('product',$args );

Now let’s understand this code.
This code will create a function called my_custom_post_product() which when called creates the custom post type(cpt),every cpt has its different option so to hold them we create an array and call it $args which will hold all the options for the custom post type .
Now we use the inbuilt function register_post_type() with two values first is the name of the custom post type to be created i.e. product and other is the array containing the list of options for the cp tie $args.
Now we call the function my_custom_post_product with inbuilt add_action. That runs the function to create the cpt.
Now we have accomplished the task of creating the cpt but it won’t be visible anywhere .Now we should create a public interface for it. For that we have to fill the $args array with options that will customize and give options to the cpt. Earlier we had created an empty $args array. For that we replace the code creating an empty $args array i.e.

$args =array();

With code that fills $args array with options i.e.:
$labels =array(
'name'=>_x('Products','post type general name'),
'singular_name'=>_x('Product','post type singular name'),
'add_new'=>_x('Add New','book'),
'add_new_item'=>__('Add New Product'),
'edit_item'=>__('Edit Product'),
'new_item'=>__('New Product'),
'all_items'=>__('All Products'),
'view_item'=>__('View Product'),
'search_items'=>__('Search Products'),
'not_found'=>__('No products found'),
'not_found_in_trash'=>__('No products found in the Trash'),

$args =array(
'description'=>'Holds our products and product specific data',

Here most of the option are self explanatory, let me explain a select few:

Will insert the new cpt at 5th position in the admin panel interface.
The above code creates 2 arrays $labels and $args to fill the options for the cpt.
Now if you open the admin panel the post type would be visible to you. Now you have a fully working cpt.

Best Practices:

Now instead of just naming your cpt generally like product you should add a prefix e.g. acme_product so that it minimizes conflicts with other cpt, that you may add in the future.
Name of cpt should not exceed 20 characters as it is the maximum length limit.
Don’t begin your cpt name with wp_

But it would use inbuilt messages when you create or update a new product like when a new post is created it would give a message like “New post created” but would not it be better if you get message like “New product created” relevant to your cpt. For that you need to code further.

Read Furthur:
Custom options, Custom Taxanomy, post meta boxes,

Comments are closed.