Custom Post Type – WordPress

Short and simple guide to creating a custom post type for a WordPress theme or plugin. There are a few guides out there that will take you through the in’s and out’s of register_post_type() such as so I will keep this short and sweet.

First of all have a quick read through the wp codex for registering post types to get a general idea of what is available to you. Head over in your functions.php or your plugins main file and create a new function and dump in this code.

function post_custom_init() {
$labels = array(
'name' => 'Custom Post',
'singular_name' => 'Custom Post',
'add_new' => 'Add New',
'add_new_item' => 'Add New Custom Post',
'edit_item' => 'Edit Custom Post',
'new_item' => 'New Custom Post',
'all_items' => 'All Custom Posts',
'view_item' => 'View Custom Post',
'search_items' => 'Search Custom Posts',
'not_found' => 'No Custom Posts found',
'not_found_in_trash' => 'No Custom Posts found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Custom Posts'

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'custom-post' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail' )

register_post_type( 'custom-post', $args );


Labels Array

The array $labels is used to configure all the text used in your new custom post type, such as buttons and headers as well as any default content on the page.

  • name’ – general name for the post type, usually plural. The same as, and overridden by $post_type_object->label
  • singular_name’ – name for one object of this post type. Defaults to value of name
  • menu_name’ – the menu name text. This string is the name to give menu items. Defaults to value of name
  • all_items’ – the all items text used in the menu. Default is the Name label
  • add_new’ – the add new text. The default is Add New for both hierarchical and non-hierarchical types. When internationalizing this string, please use a gettext context matching your post type. Example: _x('Add New', 'product');
  • add_new_item’ – the add new item text. Default is Add New Post/Add New Page
  • edit_item’ – the edit item text. Default is Edit Post/Edit Page
  • new_item’ – the new item text. Default is New Post/New Page
  • view_item’ – the view item text. Default is View Post/View Page
  • search_items’ – the search items text. Default is Search Posts/Search Pages
  • not_found’ – the not found text. Default is No posts found/No pages found
  • not_found_in_trash’ – the not found in trash text. Default is No posts found in Trash/No pages found in Trash
  • parent_item_colon’ – the parent text. This string isn’t used on non-hierarchical types. In hierarchical ones the default is Parent Page

Arguments array

The $args array does all the work for configuring how your post type works, for a full description on what each of these options does head over to the arguments section on the wordpress codex page. Really the most important bit you need to know for a standard custom post type is the supports option.

Supports give you the ability to specify what you want your custom post type to support (did you guess that?) and the values available to you are

  • ‘title’
  • ‘editor’ (content)
  • ‘author’
  • ‘thumbnail’ (featured image, current theme must also support post-thumbnails)
  • ‘excerpt’
  • ‘trackbacks’
  • ‘custom-fields’
  • ‘comments’ (also will see comment count balloon on edit screen)
  • ‘revisions’ (will store revisions)
  • ‘page-attributes’ (menu order, hierarchical must be true to show Parent option)
  • ‘post-formats’ add post formats, see Post Formats

As shown in the example code, create an array with the title of each feature you want your custom post type to support (the exact wording within the brackets in this list.)

Initialize the post type

That is just about all that you need to setup your very own custom post type, all that’s left is to have wordpress load your code. The last line in the previous sample is the wodpress function that creates your new custom post type which takes in two parameters. The first is the name of your post type, this has to be all lower case with no spaces, standard wordpress convention for naming something. This is whats used to refer to your custom post type in your theme or plugin. The second parameter is the arguments array you just created so make sure to include that.

Final thing to do is to have your flashy new custom post type function run at the right time,

add_action( 'init', 'post_custom_init' );

you can place this anywhere in your functions.php or plugin file but preferably just after the closing of the function it initializes. This just tells wordpress when to run your function.

Hopefully that gives you enough information to get started with your own custom post type if not leave a comment and I will be more than happy to help you sort it out. I will be writing about taxonomy creation and metaboxes next, these will help increase the functionality of your custom post type so stick around for that.

Author: Karlos Sarunic

24 year old Australian with a background in Software engineering and IT. I created websark to showcase my work as a software/web developer and to document my life in this chosen career.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.