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:

NB: Because this post is more than two years old, and the world of web design and technology moves on so quickly, the information in it may now be out of date

Leave a Reply

Jo was completely fantastic! I’d been struggling to make a WordPress blog deisgn work and was completely stumped. I made an enquiry to Jo about some help, and she replied within 24 hours. It was easy to agree on a price for the things that needed to be done, and she had fixed everything within 48 hours of the original enquiry. I had thought my problem was too small for someone to help with it, but I was pleasantly surprised that Jo was happy to sort out the small issues with my site for an extremely reasonable fee. Throughout the process, Jo was efficient, effective and stayed in regular communication. She answered all of my questions, even the really idiotic ones, and nothing was too much trouble. The entire experience was great and I would recommend her in a heartbeat to anyone who needs help with a website project – big or small. She is, in short, superb at what she does.

Imogen Goold