How to Create Custom Post Types in WordPress?

How to Create Custom Post Types in WordPress?

By default, WordPress comes with some post types including Posts, Pages, Attachment, Revision and Navigation Menu, etc. However, since the release of WordPress 3.8, you are allowed to create your own post types which are so called Custom Post Types.

So, how to create one? In this article, we are going to explain the reason and ways to create custom post types.

Why You Need to Add Custom Post Types?

If you want to create a movie review on your website, but you don’t have a theme that has such a dedicated movie post type coming with the content you want, then how can you achieve your goal?

You can use the regular posts. However, you can also use a custom post type. With this, you are able to post some items, like the movie’s directors, actors, genre, audience rating, some featured images and more.

How to Create Custom Post Types?

There are 2 methods to create a custom post type, one is to use a plugin, and the other is to manually create one. Now, please go on to learn the details.

Method 1 – Use a Plugin

Created for WordPress 3.x, Custom Post Type UI is a WordPress plugin that comes with an easy-to-use interface, which helps you to create and deal with custom post types and taxonomies.

First, you need to install the plugin and activate it on your WordPress site. Once done, you will find the CPT UI in the list on your WordPress left menu.

Add New Post Type

To add a post type, click “Add/Edit Post Types” and you’ll see the “Manage Post Types” page including many options as the following screenshot shows. Now, complete the necessary information on the page.

Before making any move, visit WordPress Dashicons, and there are various different icons. Then choose the icons you need to use for creating your post type.

Any icon you click in the list, you will see a larger icon on the top of the left-side area. Next to it, there are the icon’s CSS, HTML and Glyph. You should copy the CSS class.

Choose A Dashicon

Now, return to “Manage Post Type” page and then scroll down till you see the” Menu Icon” section. Next, paste this CSS class in the blank field.

Below it you will find the “Supports” section, from where you can select which sections to be displayed in the list of your custom post type, like Title, Editor, Excerpt and so on.

Fill Icon And Support Information

After all have been finished, click “Add Post Type” to save the changes. You may also click “Add/Edit Taxonomies” in the list of “CPT UI” to add new taxonomies just the way you add post type.

Besides using icons from other resources, you are also allowed to customize an icon and upload it to your WordPress site as your post type or taxonomy. To do so, click “Media” and “Add New”. Once uploaded, click the “Edit” link and copy the URL of the image. Then you just need to go to your CPT UI settings and paste the URL in the menu icon box.

Method 2 – Create Your Own Custom Post Type

This time, you need to open the functions.php file in your theme and then create a custom post type just by adding the coding stuff in this file.

First, go to Appearance > Editor.

Go to Appearance Editor

Then on the next page, click the “Theme Functions(functions.php)” on the right side. What you need to do then is to create lines of coding stuff and insert it in the textbox.

Theme Functions

A basic form of post type is very simple and it is just like this.

Basic Post Type Form

However, you can customize it according to your own needs. For example, if we want to create a custom post type named album, the following is a complete code example that made to create such a post type.

Create Post Type Code

  • labels: The labels option should be an array containing different labels that a custom post type can include.
  • description: This can be a short explanation of your custom post type. You can describe what it is and why you use it, etc.
  • public: This option controls how the type is visible to authors and readers. The default one is set to “false”. You can set this to “true”, so options included can be publicly visible.
  • menu_position: This option allows you to set the position of the custom post type in the menu. The default is null – defaults to below Comments. For example, you can set it to “10” to place it below the “Media” menu.
  • supports: By default, only the title field and editor will be shown. If you want to add support for items like revisions, comments, post formats and so on, you need to specify them here.

The above are explanations of different codes, for a full list check, you can go to the WordPress arguments section in the Codex.

After checking the code, insert it into your functions.php file and click “Update File” to store these changes. Now, you can find a custom post type named Albums on the left side menu of WordPress admin.