This is a basic tutorial on how to make your own WordPress custom post type – it is not as hard as most people expect – yet it is a very powerful capability in WordPress!

Custom post types are the types of contents your WordPress site holds. As a default, WordPress has five custom post types, which are post, page, attachment, revision and navigation menu. However, in WordPress 3.0, the developers introduced custom post types, and this means you can easily create your own. Custom post types are now widely used in themes and plugins to make it simple and convenient for users to manage all kinds of content. Example of this could be events in calendar plugins, products and deals in e-commerce plugins, slides in slider plugins and so forth. The advantage of using custom post types are obvious. First, developers get most of the administration logic and database logic from WordPress core. Secondly, users will recognize the UI and will find it easy to use.



Creting your first custom type

In order to create you first custom post type, first you need to define it and use add_action to let WordPress know about this. Let us assume we run a gadet site and want to have a Gadget custom type. Start with pasting this code in functions.php file of your theme. If you are going to play around yourself with this tutorial I recommend you keep it simple and use the default WordPress theme Twenty Twelve.

add_action( 'init', 'create_gadget_post_type' );
function create_gadget_post_type() { 
    register_post_type( 'gadget', 
            'labels' => array( 
                'name' => __( 'Gadget' ), 
                'singular_name' => __( 'Gadget' ) 
        'public' => true, 
        'has_archive' => true 

Once you have added this code, you will see the new “Gadgets” tab in the dashboard. With just a few lines of code, WordPress set up a menu item and you will now be able to add content of type Gadget to the WordPress instance.


Let try to add a few gadget posts. The custom post type only have title and content but it is no different from creating a blog post in WordPress. However, at this point there are no categories and tags.


Displaying the custom post types to visitors

Custom types use templates to be rendered just like pages and posts. In order to display both a list or archive of gadgets and the content of a single gadget you need to create two template files. The WordPress template hierarchy is quite clear on how to name the template files. You need to place them in the root of the template folder.

  • single-{posttype}.php – Custom post types will use single-{posttype}.php if it is available. If not it will fall back to single.php. This means that for the gadget example we need a single-gadget.php file. To make the test simple try to copy the content from single.php and add this to single-gadget.php.
  • archive-{posttype}.php – In the same way that posts are shown on their own archive with archive.php, custom post types will use archive-{posttype}.php if it is available. Again try copying content of archive.php into archive-gadget.php.

Now the whole idea with this is of course that we get the ability to customize the gadget specific templates to fit the requirements for our new custom content type. It is an elegant and non-invasive approach to extend WordPress and users will love it.

A very important note when you try to preview the new templates is that you may get a 404 error. This is because WordPress need to flush the permalinks. You can do this by going to Settings->Permalinks and press save. The result will look like this when you view the new custom post types:


Custom post archive for gadgets


Custom post single post view for a gadget

OK, this is a very basic example and it is done in like 5-10 minutes. However, imagine the possibilities with this feature in WordPress for plugin and theme developers. I have deliberately kept this tutorial as simple as it gets. There is a lot more to learn here and I will get back with more details later.

Author : Lars Vraa

Lars is passionate about web design, web development, SEO, social media and loves to look into new technologies, techniques, tools etc. and to write articles for tripwire magazine readers.

Pin It on Pinterest

Share This

Share This

Share this post with your friends!