How to Create Custom Post Types in WordPress

Creating a Custom Post Type – The Easy Way

The easiest way to create a custom post type in WordPress is by using a plugin. This method is recommended for beginner because it is safe and super easy.

First thing you need to do is install and activate the Custom Post Type UI plugin. Upon activation, the plugin will add a new menu item in your WordPress admin menu called CPT UI.

Now go to CPT UI » Add New to create a new custom post type.

The Custom Post Type UI plugin also allows you to create custom taxonomies.

This is why the Add new custom post type page is divided into two columns. On your left, you have the form you need to fill to create your custom post type. On your right, you have a form to create a custom taxonomy if you need one.

In the custom post type column, first you need to provide a name for your custom post type. This name cannot exceed more than 20 characters, e.g. movies, recipe, deal, glossary, etc.

In the next field, you need to provide a label for your custom post type. This label will appear in your WordPress admin bar just like posts and pages. It also needs to be plural to make sense. e.g. Movies, Recipes, Deals, Glossary, etc.

After that you need to provide a singular form for your label. This singular form will be used by WordPress to show instructions and other user interface elements.

Lastly enter a description for your custom post type. This description is simply used to describe what your post type does.

Now you can click on the ‘Create Custom Post Type’ button to add your new custom post type. That’s all.

You can also click on the Advanced Label Options and Advanced Options links to customize more options for your custom post type.

Creating a Custom Post Type Manually

The problem with using a plugin is that your custom post types will disappear when the plugin is deactivated. Any data you have in those custom post types will still be there, but your custom post type will be unregistered and will not be accessible from the admin area.

If you are working on a client site and do not want to install another plugin, then you can manually create your custom post type by adding the required code in your theme’s functions.php file or in a site-specific plugin (See: Custom Post Types Debate functions.php or Plugin).

First we will show you a quick and fully working example so that you understand how it works. Take a look at this code:

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  

What this code does is that it registers a post type 'movies' with an array of arguments. These arguments are the options of our custom post type. This array has two parts, the first part is labels, which itself is an array. The second part contains other arguments like public visibility, has archive, and slug that will be used in URLs for this post type.

Now lets take a look at a detailed piece of code that adds more options to your custom post type.

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62          

As you can see, we have added many more options to the custom post type with his code. It will add more features like support for revisions, featured image, custom fields, etc.

We have also associated this custom post type with a custom taxonomy called genres. Also notice the part where we have set the hierarchical value to be false. If you would like your custom post type to behave like Pages, then you can set this value to true.

Also notice repeated usage of twentythirteen, this is called text domain. If your theme is translation ready and you want your custom post types to be translated, then you will need to mention text domain used by your theme. You can find your theme’s text domain inside style.css file in your theme directory. Text domain will be mentioned in the header of the file.

Displaying Custom Post Types on Your Site

WordPress comes with built in support for displaying your custom post types. Once you have added a few items into your new custom post type, it is time to display them on your website.

There are a couple of methods that you can use, each one has its own benefits.

Displaying Custom Post Type Using Default Archive Template

First you can simply go to Appearance » Menus and add a custom link to your menu. This custom link is the link to your custom post type. If you are using SEO friendly permalinks then your CPT’s URL will most likely be something like this:

http://example.com/movies

If you are not using SEO friendly permalinks, then your custom post type URL will be something like this:

http://example.com/?post_type=movies

Replace example.com with your own domain name and movies with your custom post type name. Save your menu and then visit the front-end of your website. You will see the new menu you added, and when you click on it, it will display your custom post type archive page using the archive.php template file in your theme.

Using Custom Templates for CPT Archives and Single Entries

If you don’t like the appearance of the archive page for your custom post type, then you can use dedicated template for custom post type archive. To do that all you need to do is create a new file in your theme directory and name it archive-movies.php. Replace movies with the name of your custom post type.

For geting started, you can copy the contents of your theme’s archive.php file into archive-movies.php template and then start modifying it to meet your needs. Now whenever the archive page for your custom post type is accessed, this template will be used to display it.

Similarly, you can also create a custom template for your post type’s single entry display. To do that you need to create single-movies.php in your theme directory. Don’t forget to replace movies with the name of your custom post type.

You can get started by copying the contents of your theme’s single.php template into single-movies.php template and then start modifying it to meet your needs.

Displaying Custom Post Types on The Front Page

One advantage of using custom post types is that it keeps your custom content types away from your regular posts. However, if you would like them to display among your regular post, then you can do so by adding this code into your theme’s functions.php file or a site-specific plugin:

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6 7  

Don’t forget to replace movies with your custom post type.

Querying Custom Post Types

If you are familiar with the coding and would like to run loop queries in your templates, then here is how to do that (Related: What is a Loop?).

By querying the database, you can retrieve items from a custom post type.

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

In this code, first we have defined the post type and posts per page in the arguments for our new WP_Query class. After that we ran our query, retrieved the posts and displayed them inside the loop.

Displaying Custom Post Types in Widgets

You will notice that there is a default widget in WordPress to display recent posts. But this widget does not allow you to choose a custom post type. What if you wanted to display latest entries from your newly created post type in a widget? There is an easy way to do this.

First thing you need to do is install and activate the Ultimate Posts Widget plugin. Upon activation, simply go to Appearance » Widgets and drag and drop the Ultimate Posts widget to a sidebar.

This powerful widget will allow you to show recent posts from any post types. You can also display post excerpt with a read more link or even show a featured image next to post title. Configure the widget by selecting the options you want and by selecting your custom post type. After that save your changes and see the widget in action on your website.