How and Why to Create Custom Post Types

WordPress code

Custom Post Types can be immensely useful for displaying different types of information on your WordPress site.

By default there are two types of post types in WordPress; posts and pages. If you’ve tinkered with WordPress for a while, you’ll know that they’re used for different reasons and more than likely, they display your content in differing ways, depending on your theme.

However, the time may come when you have a need to display information in more than these two ways. A few good examples of this are for products if you have an e-commerce store, or for events if you are listing things like gigs or conferences.

By creating a custom post type, you’ll get a separate area in the admin area for those kind of posts (in the same way that posts and pages already have separate menus) and you can create some extra theme files to control how they’re displayed on your site.

Create the custom post type

You should think of a very unique post type name, to avoid conflicts with themes or plugins that might create similar post types. Try prefixing the name with your site name, like doitwithwp_products for example.

Now you need to actually create the custom post type within WordPress. Head on over to your plugin editor to edit your functionality plugin, or functions.php if you haven’t seen sense yet.

Paste the following code at the bottom of your functions file and then we’ll discuss all the details:

All of the $labels arguments are fairly self-explanatory; it’s just a case of creating the correct verbiage for your custom post type.

In the $args section, you can dictate how WordPress should treat the post type. For example, you can set who can edit them, what editorial capabilities should be available for that post type (things like post thumbnails, custom fields etc.) and whether you can create a hierarchy with the new post type. The Codex actually has a lot of good information on how to configure your post type just the way you need it, following the example above. And of course, if you need help understanding something, leave a comment and I’ll help you out.

Once you save this, refresh your dashboard and you should see a new menu for your new post type. Adding a new post under this post type should be a very familiar process (it’s really no different to writing a post or page).

Setting the appearance of your custom post type

You can start by copying your single.php file or page.php if the layout is similar to how you want your custom post type to appear, or if you don’t feel comfortable building a layout from scratch. The file name needs to be single-posttype.php where posttype is the name of your custom post type as you set above in your code.

If you don’t create an individual template for displaying these kinds of posts, WordPress will default to using single.php for displaying them. You can also specify how the archives should look if you like, by creating a unique archive-posttype.php.

So I’m intrigued to hear; how have you used custom post types? What problems were you trying to overcome?

19 thoughts on “How and Why to Create Custom Post Types”

  1. barak says:

    I didnt get exexactly what u mean. can u send me a link with an example of wordpress custom page? thanks!

    1. Sure. This page contains a custom post type to display images of his clients into an owner’s gallery. So the display of the item is considerably different to the normal posts or pages that exist on the site.

  2. Holger says:

    I was really exited when Worpress anounced the custom post types first. I worked thith drupal in the past and liked the ability to create different post types and theme them like I want. Tough I have to say, building a custom post type in wp isn’t straight forward for me and I am always happy to find some great tutorials on thos topic, like this one. For those, who don’t want to hassle with php code, there are some plugins available to create and theme custom post types.

    1. Hi Holger. You’re absolutely right. Custom post types are very useful and were helpful for bringing WordPress up to par in an area where they deficient compared to other CMSs. It’s not particularly easy to create a custom post type, but if you just really can’t handle the coding, then there are ample plugins that will do the job for you.

  3. Richard says:

    This is a really interesting idea. I’ve generally just used pages and posts, but there are a couple of custom templates that I use on occassion. The homepage on my blog is a custom template that displays random posts with a news slider. This kind of custom page can be really good for a more unique home page.

  4. Unknown says:

    Coding is fun but if you can have all these done by using a plugin, better. Custom post types are pretty useful in WordPress and I’m happy that this feature was finally integrated in newer versions.

    1. Well, not necessarily. There’s nothing more efficient than the raw code, so if you can accomplish it that way, it’s less burdensome on the server and your site will run faster. That said, the difference can be negligible depending on the plugin, but the fact is, repeated several times over, it can start to have an impact.

  5. Joshua Chase says:

    This might be one of the simplest yet informative posts on custom post types. I’m copying this to my evernote for safe keeping. Great read, I’m trying to get away from custom templates and use custom post types so when changing out themes there is less work to be done. Thanks again.

  6. Dave Porter says:

    Thanks for the great article…
    I have observed that creating and editing custom post types, custom taxonomies & meta-boxes etc is fairly straight forward to setup, configure and edit (by hand or via a plugin).
    But getting that info displayed in your theme is poorly documented and the more challenging aspect of this great WP feature!
    Regards, Dave

  7. Vlad Ionescu says:

    You don’t have to set publicly_queryable and show_ui to true if public is true :) They default to whatever public is set to.

    Very good and to the point article, I was scared of custom post types before this, but you’ve shown me how easy it is! This is going to dramatically change how I develop for clients. It will be much easier now to have different posts displayed differently without having clients confused about page templates.

    1. Well, you’re right, but I wanted to show the variables, to show that they can be configured individually. Thanks for your comment and I’m glad you found it useful.

  8. Sumon says:

    Hi
    May be there is a mistake. The code will be
    add_action (‘init’, ‘doitwithwp_evernt_register’);
    instead of
    add_action (‘init’, ‘doitwithwp_giveaway_register’);
    Am i right or wrong?

    1. You’re right. That was left over from a previous project – my mistake. Except you had a slight typo in yours, so it should be:

      add_action (‘init’, ‘doitwithwp_event_register’);

      I’ll update the tutorial. Thanks very much

  9. Ojibirish says:

    I am looking at building two custom post types, one for my web portfolios and one for graphic stuff I have done. Do I have to created to custom templates for each web and graphic and then set up the custom post to post to the proper page of web or graphic? If I am right what code do I put on to each custom template to get the custom posts to go to each page? I hope I am not confusing you on this lol. Apologies if I do.

    1. Well, it is a little confusing, but you’ll need to set up two individual custom post types, using the code below (one for each CPT). Then you can create a unique page template with a loop that pulls in both custom post types, if you want to show them on the same page.

      1. Tim says:

        I got the code put in for each CPT and and created a template page for each portfolio page one for web stuff and the other for graphics. is there anywhere I can go to figure out the loop I would need for each page then? I am trying to learn this stuff for so I can know how to use it for clients later on. Thank you for your help so far.

        1. I think you’ll find the answers you are looking in sections 2.4 and 2.5 of this article in the Codex. It tells you which template files you’ll need, and where you’ll be able to display each of the custom post types. You’ll only need a custom loop if you want to show both custom post types on the same page.

  10. Sumon says:

    Hi
    There is an unexpected semi-colon in the following code
    ‘not_found_in_trash’ =>; __(‘No events found in Trash’),

    thanks for nice article.

    1. Thanks Sumon. Another good catch. I’ll revise it now :)

Leave a Reply