Create a Custom Post Type in WordPress

Date Posted: April 28, 2014, 10:57 am


Suppose you want your website to have a section for Game Articles. 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 an administration menu and a dedicated editing page for your blog posts.

custom-post-type

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

Bellow is a list of Default Post Types in WordPress.

Post (Post Type: ‘post’)
Page (Post Type: ‘page’)
Attachment (Post Type: ‘attachment’)
Revision (Post Type: ‘revision’)
Navigation menu (Post Type: ‘nav_menu_item’)

A custom post type can be added to WordPress via the register_post_type() function. This function allows you to define a new post type by its labels, supported features, availability and other specifics. Do pay close attention to not having your custom post type identifier exceed 20 characters, as the post_type column in the database is currently a VARCHAR field of that length.

So let’s register a new post type:

1
register_post_type('articles', $articles_args);

The register_post_type function does most of the work for us. As soon as it is called it prepares the WordPress environment for a new custom post type including the different sections in the admin. This function takes two arguments: the first one is an unique name of the custom post type (for this tutorial, we called it “articles”) and the second one an array demonstrating the properties of the new custom post type.

Now let’s define the properties of our post type:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
$articles_args = array(
    'labels'                => array(
        // These are all the labels of our custom post type, you do not have to worry about how you going to layout or create each menu because WordPress already does that for us.
        'name'                  => _x('Game Articles', 'post type general name'),
        'singular_name'         => _x('Game Articles', 'post type singular name'),
        'all_items'             => _x('My Game Articles', 'post type name'),
        'add_new'               => _x('Add New', 'articles'),
        'add_new_item'          => __("Add New Article"),
        'edit_item'             => __("Edit Article"),
        'new_item'              => __("New Article"),
        'view_item'             => __("View Article"),
        'search_items'          => __("Search Article"),
        'not_found'             => __('No articles found'),
        'not_found_in_trash'    => __('No articles found in Trash'),
        'parent_item_colon'     => ''      
    ),
    'public'                => true,
    'publicly_queryable'    => true,
    'show_ui'               => true,
    'query_var'             => true,
    'rewrite'               => true,
    'hierarchical'          => false,
    'menu_position'         => null,
    'capability_type'       => 'post',
    //Define the list of meta boxes that our post type is going to support.
    'supports'              => array('title', 'excerpt', 'editor', 'thumbnail', 'comments'),
    'menu_icon'             => get_bloginfo('template_directory') . '/images/custom-post-icon.png', //16x16 png if you want an icon
    'map_meta_cap' => true,
   
    /*
    Set which capabilities will be associated to our new custom post type.
    By default, seven keys are accepted as part of the capabilities array:
        - read - Controls whether objects of this post type can be read.
        - delete_posts - Controls whether objects of this post type can be deleted.
        - delete_private_posts - Controls whether private objects can be deleted.
        - delete_published_posts - Controls whether published objects can be deleted.
        - delete_others_posts - Controls whether objects owned by other users can be can be deleted. If the post type does not support an author, then this will behave like delete_posts.
        - edit_private_posts - Controls whether private objects can be edited.
        - edit_published_posts - Controls whether published objects can be edited.
   
    I have already defined bellow all the capabilities that we will be needing for our custom post type. Please do note the use of singular and plural (article, articles).
    */

    'capabilities' => array(
        'edit_post'                 => 'edit_article',
        'read_post'                 => 'read_article',
        'delete_post'               => 'delete_articles',
        'edit_posts'                => 'edit_articles',
        'edit_others_posts'         => 'edit_others_articles',
        'publish_posts'             => 'publish_articles',
        'read_private_posts'        => 'read_private_articles',
        'delete_posts'              => 'delete_articles',
        'delete_private_posts'      => 'delete_private_articles',
        'delete_published_posts'    => 'delete_published_articles',
        'delete_others_posts'       => 'delete_others_articles',
        'edit_private_posts'        => 'edit_private_articles',
        'edit_published_post'       => 'edit_published_article'
    ),
    'capability_type' => array('article', 'articles')
);

Add your custom admin columns (Optional):

1
2
3
4
5
6
7
8
9
10
function mmo_add_new_articles_columns( $columns ){
    $columns = array(
        'cb'                =>        '<input type="checkbox">',
        'title'             =>        'Game Article Title',
        'author'            =>        'Author',
        'date'              =>        'Date'
    );
    return $columns;
}
add_filter('manage_edit-articles_columns', 'mmo_add_new_articles_columns');

About author


Carl Victor Fontanos

A fine gentleman specializing in front-end and back-end development with extensive experience building high performance web applications that keeps users engaged and help businesses grow.

 
Hire Me