May 17, 2014 admin

A Guide to WordPress Custom Post Types

Creation and Display

WordPress is built for customization. It was created in such a way that each and every section is customizable. In this tutorial we will be exploring one of the most powerful features of WordPress known as Custom Post Types and how WordPress reached a new height with the advent of this wonderful feature.

What Actually Are Custom Post Types?

Suppose you want your blog to have a separate section for Portfolio. By using Custom Post Types you can create a new type of item like Posts and Pages, which will contain a different set of data. It will have a new administration menu, dedicated editing pages, custom taxonomies and many more utilities required for full fledged publishing.

Custom Post Types are a new set of administrative options appearing along with the default post types such asPosts, Pages, Attachments etc. A Custom Post Type can store any type of information. It has a dedicated editor, media uploader and uses the existing WordPress table structure for ease in data management. The main advantage of creating custom post types using the WordPress API is that it equips itself well with existing themes and templates. Custom Post Types are also SEO friendly because of their nifty permalinks.

Why Use Custom Post Types?

Custom Post Types help us to keep different types of posts in different buckets. It separates our regular posts from others. Simple enough!

What WordPress Can Do For You Now

A custom post type is nothing more than a regular post with a different post_type value in the database. The post type of regular posts is post, pages use page, attachments use attachment and so on. You can now create your own to indicate the type of content created. You could create custom post types for books, movies, reviews, products and so on.

If created correctly, you can achieve the following with a few lines of code:

The custom post type will show up in the back end as a separate menu item with its own post list and “add new” page

Navigating to will take you to the archive page for the post type. This is akin to visiting the front page for latest posts from the “post” post type.

Categories and tags can be made available to the custom post type, or you can create custom taxonomies.

Apart from these, you can modify countless options, such as where the custom post type should be placed in the menu, should it be searchable, which user level can access it, should it be hierarchical, custom rewrite rules, etc.

Let’s Create a Custom Post Type

For our tutorial we’re going to be editing functions.php which is your theme directory. We’re simply adding to this file, so feel free to start at the top or at the bottom – just don’t change any of the code that’s already there. For the more confident of you out there you could adapt this code into a plugin.

With a few edits we are going to create a custom post type for our portfolio, and a template which will use this information. We’ll end up with a new panel in the admin menu which looks like this:

A new overview page called “My Portfolio”:


And a new place to enter our content:

Here’s the first bit of code we need to add to functions.php, which I’ll review below.

Step 1: Register Custom Function

add_action(‘init’, ‘portfolio_register’);

We’re adding an action when the WP Admin initializes to call the function portfolio_register().

Step 2: Function Implementation

Provide an implementation of the  portfolio_register function.

function portfolio_register() {
        $labels = array(

‘name’ => _x(‘My Portfolio’, ‘post type general name’),

               'singular_name' => _x('Portfolio Item', 
               'post type singular name'),
               'add_new' => _x('Add New', 'portfolio item'),
               'add_new_item' => __('Add New Portfolio Item'),
                'edit_item' => __('Edit Portfolio Item'),
               'new_item' => __('New Portfolio Item'),
               'view_item' => __('View Portfolio Item'),
               'search_items' => __('Search Portfolio'),
               'not_found' => __('Nothing found'),
            'not_found_in_trash' => __('Nothing found in Trash'),
               'parent_item_colon' => ''
        $args = array(
               'labels' => $labels,
               'public' => true,
               'publicly_queryable' => true,
               'show_ui' => true,
               'query_var' => true,
               'menu_icon' => get_stylesheet_directory_uri() .
               'rewrite' => true,
               'capability_type' => 'post',
               'hierarchical' => false,
               'menu_position' => null,
               'supports' => array('title','editor','thumbnail')
        register_post_type( 'portfolio' , $args );

In the function portfolio_register() we create two arrays, $labels and $args, and then use register_post_type  to pull it all together. In doing so we name the new custom post type ‘portfolio’ and tell it to use the arguments from $args.

First let’s look at $labels:

name this is the (probably plural) name for our new post type singular_name how you’d refer to this in the singular (such as ‘Add new ****’) The rest of $labels simply refer to different circumstances in which the name of your custom post type would be used.

And now $args:

public should they be shown in the admin UI show_ui should we display an admin panel for this custom post type menu_icon a custom icon for the admin panel capability_type WordPress will treat this as a ‘post’ for read, edit, and delete capabilities hierarchical is it hierarchical, like pages rewrite rewrites permalinks using the slug ‘portfolio’ supports which items do we want to display on the add/edit post page

Step 3: Custom Taxonomies

The next thing we need to do is register a taxonomy. Or, in English, create categories for this new content type. For example, in our portfolio we want to include the names of technologies and software used to create our work. I’m going to call this taxonomy ‘Skills’, and populate it with things like HTML, CSS and jQuery. It’s just one line of code:

register_taxonomy("Skills", array("portfolio"), array("hierarchical"
 => true, "label" => "Skills", "singular_label" => "Skill",
 "rewrite" => true));

The first item here is the taxonomy name, ‘Skills’. The second is the name of the object type we’re applying it to, in our case the custom post type ‘portfolio’ (which is an array). Finally our arguments; you can find a full list at, and here we’re using just three which have the same meaning as described in Step 2.

Add that line of code in and you should now see:

You can then enter new ‘skills’ just like you’d enter categories for blog posts. It looks like this:

Tagged: ,

Leave a Reply

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