How to create a Custom Post Type? Full guide with steps

WordPress is a great tool for creating functional websites. Most people consider WordPress as just a blogging platform. But the truth is wordpress is an extremely powerful platform to create different types of fully functional websites. Each new release of wordpress includes many features to enhance functionality of your WordPress site. In this article we will explore one such feature called ‘custom post types’ in WordPress. WordPress added this in version 3.0 onwards

What is custom post type? Do I need it?

Basically WordPress is designed to be a blogging platform. When it comes to creating content for your WordPress site, you can write either posts or Pages. By default, WordPress comes with certain post/page writing structure. i.e. your post title in the upper field, post body content in the main post editing box below it, select a category, add tags etc. In some situations, you want to create something different than this.

Examples of Custom Post Types
For example, you have a tech blog where you write IT-related news, events, updates etc. Now you want to add a product review section to your site and you do not mix this with regular blog content. You want to create a separate section for these Reviews. I’m sure  you want to use a different structure and different set of data inside your review post. You can extend the existing WordPress infrastructure and accomplish this using ‘custom Post types’ along with ‘custom fields’.

Custom Post types allow you to create new content sections on your site. It will add a new administration menu, dedicated editing pages, custom taxonomies and many more utilities required for full fledged publishing.

Some other examples are:

  • A food blogger wants to rate recipes book post
  • A music blogger wants add a post to sell music album cds
  • A travel blogger wants to list popular  tourist destinations
  • Many popular WordPress plugins also use custom post types. Some examples are : Countries Plugin, WP e-Commerce Plugin,
examples

examples

Make A Product Review With WordPress: Custom Post Types & Custom Fields

In this article I’ll explain how to create a custom post with an example. Let us see how to create a product review custom post. We’re going to add a new section of our website “Product Reviews” that is dedicated for product reviews. You can implement this using custom post types and custom fields. Please note that you’ll need to be familiar with PHP to adjust code based on your needs.

Step 1: Create a Custom Post Type

The easiest way to create a custom post type in WordPress is by using a plugin called Custom Post Type UI .

  1. Install & Activate Custom Post Type UI plugin. (Refer the article How to install a WordPress Plugin)
  2. Upon activation, the plugin will add a new menu item in your WordPress admin menu called CPT UI
  3. Go to CPT UI » Add New to create a new custom post type.

    Add new

    Add new

  4. In this page you can see two columns. Custom Post Type UI plugin allows you to create custom post types (On your left) & custom taxonomies (On your right).

    Create custom post type

    Create custom post type

  5.  Create Custom Post Type – On your left, fill out the Post Type Name, Label fields and description. At the bottom you can see 2 links “Advanced Label Options & Advanced Options” . You can leave the default, or you can click on it to set new values.  After filling click on the ‘Create Custom Post Type’ button to add your new custom post type.Now you can see new menu item in your WordPress admin left menu bar (just like posts /pages menu). The label entry will appear in your WordPress admin left menu bar.  (See fig below). In my case I used the text “product reviews”.

    Post type label

    Post type label

  6. Custom  taxonomies – WordPress uses taxonomies to group post together. Two popular wordpress taxonomies are Categories and Tags. When you create a custom post type called product review, even though you can use categories, you may not want to mix this review with regular blog because they are used differently. So it is a good to create custom taxonomies to group custom posts.For example, create a new custom taxonomy called Review categories. Then you can add topic terms like: Computer, TV, Phone, etc. This would allow you and your users to sort reviews by each topic.Custom Post Type UI plugin allows you to create custom taxonomies. Go to CPT UI » Add New. In this page you can see fields to create custom taxonomies (On your right). Enter the fields Taxonomy Name, Label and Attach to Post Type( Note: Since I’m creating this custom taxonomy for custom post type, I selected Product reviews).
    create taxonomy

    create taxonomy

    At the bottom you can see 2 links “Advanced Label Options & Advanced Options” . You can leave the default, or you can click on it to set new values. I just want to mention one option listed there >> hierarchical == false.

    Now You can see new menu item to add Review Categories. Click on it and create new topics.

    Add Review Categories

    Add Review Categories

    create custom taxonomy

    create custom taxonomy

  7. Now your custom post type is ready. From your WordPress admin left menu bar click on your newly created custom post type( In my case I used the text “product reviews”). Here you will see a regular post writing page on WordPress. But when you create a product review, you need to add additional information associated with each product. For e.g. a database of digital cameras for instance might need:
    • Pros
    • Cons
    • Purchase Link
    • Rating

    Rather than adding this info directly to the description of the product, It is wise to create custom fields to hold this info.

Step2: Adding Custom Fields to a Custom Post Type

Since WordPress native custom fields capability is not enough for our need, we are going to use a third-party plugin (read A Guide to WordPress Custom Fields). Here I’m using Advanced Custom Fields plugin. Advanced Custom Fields is a free plugin that let you create different types of custom fields, create them as a group and then assign that group to your custom post types. Many add-ons also available for this plugin. For e.g. if you want to use star rating , use this acf add-on.

  • Download, install and activate Advanced Custom Fields. (Refer the article how to install a plugin)

    Advanced Custom Fields

    Advanced Custom Fields

  • After installing you will see custom fields menu link on left sidebar.
    custom fields

    custom fields

    Here you can create a set of fields(called field group) and then assign this to your custom post types.

    custom fields

    custom fields

    Click on “Custom Fields” and create a field group called “Product Review” . Click “Add Field” to create the fields needed. In this example I’m adding fields like “Pros”, “cons” etc.

  • In the “Location” section, you can assign this field group to your product review custom post type.

    Custom fields

    Custom fields

  • In the “Options” section, you can configure how you want your field group to be displayed. You can also decide which all other default WordPress inputs to be hidden when the field group is displayed.

    Display Options

    Display Options

Create a product review

Now that you created your cusom post and template, we will now see how you can create your custom post:

Post type label

Create new product review

From your wordpress admin left menu Click Product Reviews >> Add Product Review.

Create product review

Create product review

Write and publish a product review. But when you view the post that is just created, WordPress template hierarchy will use default single.php template file. There these custom fields values will not be displayed. So next step: we need to work on a template that includes these custom post types into our site so they display properly. To display these custom posts, you need to either edit default template file or create a custom single post template.

Step3: Create Single Post Templates for Custom Post Type

Let us see how you can create a custom single post template. To display your Custom Post Type you need to create a new file name single-YOURCUSTOMPOSTNAME.php in your theme folder. So let’s say your custom post type is called”Productreview”, then you need to create single-productreview.php to display your single custom post.

Duplicate single.php

Duplicate single.php

Make a copy of the single.php file in your theme directory. Rename it to single-productreview.php. This isn’t something you can do from the wordpress dashboard. You need to copy the file using your hosting control panel or FTP application.

  • Refer this article to see how to FTP it to your theme directory.
  • Refer this article to see how to make a copy of single.php using cpanel.

Now go back to Appearance > Editor in your WordPress dashboard, select your theme, and the single-productreview.php file name in the right sidebar. Here you can add code to display custom fields or customize look and feel.

 

Displaying Custom Fields
To display the Custom Fields, Put the following tag within The Loop. Find where your main WordPress Loop originates.

loop starts

loop starts

Call the wordpress function  get_post_meta().

custom fields

custom fields

Assign the name of the key that you want to appear. For example in my case, I want to display custom field called “pros”.

I hope, by now you understood about custom posts are and custom fields. There  are many plugins available to enhance WordPress posting abilities. I suggest doing your own research and find the best solution to your need.

 

Web Hosting

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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

*