How to: Create a WordPress Custom Post Type

1st January 2014

For a long time it’s been possible to set up your own post types to handle different kinds of content, but in WordPress 2.9 this process was simplified with the inclusion of the register_post_type() function. Here’s a really basic guide to setting one up, using the example of an events post type.

Plugin Versus functions.php

There are two ways to integrate your new custom post type. The first is to write it directly into your theme using functions.php, the second is to create a plugin. Both will work just fine, but the latter is advisable if you want to retain the flexibility to change themes in the future without having to hack it about each time and re-include all the necessary code. For that reason we’re going to create a plugin in this example.

The Basics of a Plugin

Creating a plugin is pretty simple. First off you’ll need to find the wp-content/plugins folder, create a new folder within and name it appropriately. I’m going to call mine ‘jcd-events’.

Next create a new php file inside this folder and paste in the following code:

NB: You’ll re-use this same format whenever you create a new plugin.

Registering the Post Type

The next step is to register the actual post type. In its basic form this is what the code for doing that looks like:

We’re going to want to define a few more settings than that though.

Paste this into your php file and hit save. Now if you head to your dashboard and navigate to plugins you should see ‘Events plugin’ appear. On activation a new menu item will appear, in this case called ‘Events’. You’ll be able to list all of your event posts and your post type will have it’s own ‘add new’ page.

Once you’ve got these basics working you can then start to look at adding further features like custom taxonomies, to extend your plugin further. For more information visit the WordPress Codex:

http://codex.wordpress.org/Function_Reference/register_post_type

We couldn’t believe our luck when Jo offered to build us our website for free. We were even more astounded when only weeks later we had a great site, purpose build to our needs. We couldn’t have paid for a better site or more fluid an experience of commissioning a website.

(Emma Gordon (Centre Manager, Bicester Green))