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:

I am not exactly a technophobe but due to previous experiences of pressing buttons that I have no business pressing I have shied away from doing thing to update or improve my site. I have called on Jo Cox’s expertise several times and she is always quick, efficient and gets you back on track whilst also calming you down. I can honestly say that there is no one else I would call on in an emergency to sort out problems with my website. I have never had a bad experience with Jo’s services or with Jo as the service provider.

Lisa Bentley