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:

Leave a Reply

What our customers say:

As a freelance musician and teacher my website is very important for generating business and maintaining good contact with my clients. I have found Jo Cox Design very useful to help maintain elements of my site which are more complex. I like to have control of my site but I often find there are things I cannot achieve on my own. I would certainly recommend Jo as a friendly person who’s easy to work with and who can help you achieve such goals.

Trev Williams