HTML
Introduction To Custom Post Types in WordPress 3 – Part 1
November 19, 2010
0

Custom Post Types functionality is added in WordPress 3 (it was present in 2.9 but with limited featrues). Broadly speaking, with
Custom Post Types, your Posts (also commonly referred as blog-enty” or “blog-post”) are no longer limited to the two predefined types of Post and Page and most likely you end up with the standard form for every post.

So instead of this standard Menu:

You can have something like this, where Books and Tapes are examples of custom post types.

There are plug-ins and other ways to customize and extends the forms and templates for Post, but they usually require fairly good understanding of WordPress inner working. Using Post Types is simpler.

With Custom Post Types, you can define your own post types. Let’s say you have a programming portfolio-store site, you can create a post-type for Tutorials, Demos, Papers, etc. Each of these post types can have their own distinct form and template layout.

The function you will be using to do this is

register_post_type($postType, $arguments)</pre>

Where $postType is a string identifier for your custom post type and $arguments is an associative array containing things such as ‘name‘, ‘menuPosition‘, ‘description‘ and more. which you can read about here.   At the minimum, you should specify the ‘labels‘ and ‘show_ui‘ argument in order for the post type to appear in the WordPress menu.

Adding to functions.php

The simplest way to add a custom post type is to add the type in functions.php in your theme folder. Go to your theme folder, usually sites/all/themes/themeName, where you should find a file named functions.php. Open functions.php for editing and add something like below:

function create_my_post_type()
{
register_post_type('books',
array(
'labels' => array(
'name' => __('Books'),
'singular_name' => __('Book'),
),
'show_ui' => true,
'capability_type' => 'post',
'public' => true,
)
);
}

add_action('init', 'create_my_post_type');

This bootsrap tells WordPress to call create_my_post_type() during initialization which then calls register_post_type(). The result is like this:

You can have more than one types in this bootstrap function, like for example:

function create_my_post_type()
{
register_post_type('books',
array(
'labels' => array(
'name' => __('Books'),
'singular_name' => __('Book'),
),
'show_ui' => true,
'capability_type' => 'post',
'public' => true,
)
);

register_post_type('tapes',
array(
'labels' => array(
'name' => __('Tapes'),
'singular_name' => __('Tape'),
),
'show_ui' => true,
'capability_type' => 'post',
'public' => true,
)
);
}

add_action('init', 'create_my_post_type');

Which produces this menu:

Using Plugin

The problem with adding this into functions.php is that the changes you made will be lost if you switch to other themes. The alternative is to create a Plugin that adds the custom type during initialization. This sounds intimidating but it’s not as bad as you think. Here’s an example.

Let’s say I want to create a plugin in permadi-custom-post-types folder.

  1. Goto the plugin folder (wp-content/plugins/).
  2. Create a folder named permadi-custom-post-types.
  3. Inside permadi-custom-post-types, create permadi-custom-post-types.php
  4. In permadi-custom-post-types.php, enter the header, like this:
    <?php
    /*
    Plugin Name: Permadi Custom Type
    Description: My example bare-bone plugin.
    Author: F. Permadi Version: 1.0
    */
    ?>
    
  5. Refresh your Plugin list, you should see the plugin.
    Do not Activate the plug-in yet (well, you can, but it will just do nothing).
  6. Now in permadi-custom-post-types.php, enter the register_post_type() call(s). I suggest not using generic function name such as create_post_type but customize it so that there’s no naming conflict later on, like below
    function create_permadi_com_post_type()
    {
    register_post_type('books',
    array(
    'labels' => array(
    'name' => __('Books'),
    'singular_name' => __('Book'),
    ),
    'show_ui' => true,
    'capability_type' => 'post',
    'public' => true,
    )
    );
    
    register_post_type('tapes',
    array(
    'labels' => array(
    'name' => __('Tapes'),
    'singular_name' => __('Tape'),
    ),
    'show_ui' => true,
    'capability_type' => 'post',
    'public' => true,
    )
    );
    }
    
    add_action('init', 'create_permadi_com_post_type');
    
    
  7. Activate the plugin and you should see the custom types:

    You can download the sample module here: /tutorial/custom-post-types-wordpress-permadi-example/permadi-custom-post-types.zip
    In the next tutorial, we will see how to customize the forms for these custom types: https://www.permadi.com/blog/2010/11/introduction-to-custom-post-type-in-wordpress-3-part-2/