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?