How to Create a WordPress Custom Post Type in 5 Minutes

June 21, 2013 · 4 comments

by Lars

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.


ElegantThemes
ThemeForest

Advertisement

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', 
        array( 
            '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.

gadget-type-added

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.

create-gadgets

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:

archives

Custom post archive for gadgets

single

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.

{ 4 comments… read them below or add one }

Andrei Josan June 23, 2013 at 3:43 pm

Thank you very much!
I’ve been trying for some time to create a “coupons” custom post type and now I’m pretty sure I understood the process and will manage solving this.
Thanks! Looking forward to seeing more WordPress tutorials and tips.

Reply

Brad d June 19, 2013 at 4:09 pm

Look forward to reading more articles on custom post types soon Lars!

Reply

Wordpress June 17, 2013 at 11:28 am

WordPress is no doubt that the best platform for blogging and CMS today. Creating custom post is great idea.

Reply

Chinterest June 14, 2013 at 11:14 pm

WORDPRESS , SO GOOD !

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous post:

Next post:


Web Analytics