Search

Creating Custom Post Types and adding Custom Fields

Listen to this article

While developing a website on WordPress, you divide the content you want to present, into different post types. Pages, posts, attachments, revisions and navigation menus are default post types in WordPress. Usually, most WordPress websites are built by dividing the content between these default post types. For example, you create a home page, about us page, contact page, your articles are presented as posts, you provide menus for navigation, etc.

Custom Post Type

What is a Custom Post Type?

Sometimes, there maybe a scenario, when the default post types do not suffice your requirement. You would prefer having your own post type, to identify and customize certain content. Such a content type, which is not provided by default, and created by a user, is known as a custom post type.

 

When to create a Custom Post Type?

It’s easier to understand this with an example. Let’s say your website sells shoes, and discusses recent fashion trends.

The articles you write about the fashion trends, would have to be added as post type posts, and WordPress will take care of displaying this in your blog page. Now, you wouldn’t want to sell your shoes, in the same page where your articles are listed. You would prefer having them on a separate page, maybe a shop page.

The simplest way to achieve this, would be to create a custom post type for shoes (maybe named ‘Shoes’), and handle the display of this post type yourself. Each shoe you want to sell can be then added under post type Shoes. With this you will be able to identify and handle the content related to shoes, according to your preference.

Many plugins create their own custom post types, to add and maintain certain data. Like WooCommerce has Products, or WordPress Auction Plugin has Auctions, as custom post types.

 

Creating Custom Post Types

A custom post type can be created in two ways.

  • Want total Control

If you are a developer, the register_post_type function can be used to create your own post type. You could create a plugin to register your new post type, or you can make it a part of your theme.

  • Don’t stress, use a plugin

There is the Custom Post Types UI plugin, which can make your job much easier. It provides you a very simple interface to create custom post types from your dashboard.

Custom Post Type UI

Additionally, the plugin also allows you to create custom taxonomies. Custom taxonomies provide a way to group your content. Custom taxonomies do not necessarily have to be present when using custom post types, they can be used to group any post type, hierarchically and logically.

In our example, or our custom post type shoes, we could create custom taxonomies for color, brand, type etc. These taxonomies can also be used to filter your content.

Custom Taxonomy UI

[space]

Adding Custom Fields to Post Types

Default fields for post types, such as title, author, excerpt, etc, are added by WordPress. All post types need not have the same fields. At times, you would want to add some additional fields for a post type. For example, while adding information for each shoe, you would also want to add a price, in a separate field.

  • Code your Way to Glory

You can do this by creating a metabox. A metabox is a user interface element in the admin area where you can enter data. Each additional attribute you would want to create will be a separate metabox.

  • Want to Save some Development Time

Alternatively, you could use the Advanced Custom Fields plugin, to help you. It provides you with an easy-to-use interface to create custom fields, which you can add to any post type.

Custom Field

Custom Post Type And Field

[space]

And there you have it, using the above means, you can tailor make your custom post type to address any requirement. Do note however, creating custom post types, does not ensure their display. Displaying custom post types by creating a custom display page will be explained in another article.

Sumit Pore

Sumit Pore

2 Responses

  1. Hey bro!

    Great article! Where can I find the “other article” about the – Displaying custom post types by creating a custom display page? 🙂

Leave a Reply

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

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads