Become Wordpress Professional - Your Ultimate Wordpress Guide

Tuesday, 7 January 2014

How to Create Custom Post Type in Wordpress

Creating Custom post type in wordpress is very handy. Follow along with the following tutorial and you will be able to create your own custom post type in wordpress.

how to create custom post type in wordpress
Before we begin, assume that I have a site that is to contain reviews about different things including products,books,website and it requires a new custom post type "Review" to be registered. I could have just written my reviews as default post type: "Post" but I want to keep my blog posts and reviews separate. Therefore here is the tutorial on how I can create my own custom Post type "Review".

First of all log into the back-end panel of your wordpress site and navigate to "Appearance" > "Editor" and select "Functions.php" file of your theme.

Copy the Code below and paste it at the end of functions.php:

add_action( 'init', 'create_post_type' );
function create_post_type() {
 register_post_type( 'my_reviews',
   'labels' => array(
   'name' => __( 'Reviews' ),
   'singular_name' => __( 'Review' )
  'public' => true,
  'has_archive' => true,
  'rewrite' => array('slug' => 'review'),

View PHP code on Github

Here is the description of the code above:
We created a function called "create_post_type" that contains the core function of wordpress called "register_post_type" which takes two parameters, first is the name of the custom post type which in this case is "my_reviews" and the second argument is an array that contains information about my custom post type such as it's name, singular name, it's visibility (in this case it is public), it has archive and a rewrite rule, the URL will be having "review" as the middle segment of each Review Page URL. Then we are using 'init' wordpress hook to execute this function at a particular time.
Click here to read more about register post type function on official wordpress codex

Example: The URL of review page of  Harry Potter Novel will look like:

We can also have our review post type URL's type not to contain "review" as the middle parameter for example:

For that we just need to eliminate the 'rewrite' parameter that we passed in the function in the code above.

Now save the file and you will be able to see a new item on navigation menu of your wordpress admin panel named "Reviews" as shown in in the image below:

An Important thing to keep in mind here is that we need to reset our permalinks to run the permalink flush function that is very important otherwise the new reviews of your custom post type will keep on redirecting to the homepage.

To Reset the permalinks, simply navigate to "Settings" > "Permalinks" and click "Save Changes" button.

That is it, now you have created your own custom post type. You can use the comments section below to give your feedback or ask any questions.


Post a Comment

Please Avoid Spamming. Comments will be moderated before they are published.