The dashboard view of a custom post type can be set to look however you need.
The dashboard view of a custom post type can be set to look however you need.

Registering Custom Post Types

| 0 comments

Registering Custom Post Types is a breeze, really. You just need to set all the variables that are going to govern how your custom post type should function, and put that code in either a plugin, or your theme, depending on which is more appropriate (it’s usually going to be a plugin by the way, even if you need to create a new one).

The Codex has a very helpful article on the subject, and I’m going to walk you through it with an example to show how versatile it is. In my example, I created an order tracking system for one of my clients, and as you can see from the screenshot below, custom post types don’t have to look anything like a regular post; you can tailor them to your every need.

The dashboard view of a custom post type can be set to look however you need.

The code behind it

In order to register a custom post type, you need to use the register_post_type function. Here is the code that I used to build the order system shown above:

function register_hbos_orders_cpt() {
	$args = array(
		'label' => 'Orders',
		'labels' => array(
			'name' => _x('Orders', 'Post type general name'),
			'singular_name' => _x('Order', 'post type singular name'),
			'add_new' => _x('Add New', 'order'),
			'add_new_item' => __('Add New Order'),
			'edit_item' => __('Edit Order'),
			'new_item' => __('New Order'),
			'all_items' => __('All Orders'),
			'view_item' => __('View Order'),
			'search_items' => __('Search Orders'),
			'not_found' =>  __('No orders found'),
			'not_found_in_trash' => __('No orders found in Trash'), 
			'parent_item_colon' => '',
			'menu_name' => __('Orders')
		),
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
		'show_in_menu' => true,
		'show_in_nav_menus' => false,
		'query_var' => true,
		'rewrite' => true,
		'capability_type' => 'post',
		'has_archive' => true,
		'hierarchical' => false,
		'menu_position' => 2,
		'supports' => array( 'title', 'author', 'revisions' )
	);
	register_post_type( 'hbos_orders', $args );
}

add_action( 'init', 'register_hbos_orders_cpt' );

So, first thing to note is that the custom post type name is hbos_orders. That can be seen in register_post_type function near the bottom of the code. Everything above that is setting the variables that determine how the post type should function. For example, you can set all the terminology that will be used for the post type when they’re viewed in the dashboard and on the site (such as View Order, Edit Order and Add New Order). You can also set whether the post type should have it’s own dashboard menu, whether is should be searchable, viewable by the public, have archives and whether you should be able to create a hierarchy structure (create parent and child items).

The supports argument is useful, because you can dictate what elements of the normal post editing window should be permitted on your post type. In this example, I’ve only allowed the title, author, and revisions, which means that the tag window, category window, post thumbnail box and even the editor window will not be shown – this is because I am using custom meta boxes to save the information I want to use.

The example above only contains some of the variables that you can set, so I strongly recommend that you check out the Codex article to see what other variables you can add to set up your own custom post type.

One last thing that you’ll want to do is flush the rewrite rules, but only once the plugin is activated (flushing rewrite rules is very server intensive and should not be do on every page load, on init, or admin_init for example). To do so, add a function like this below your function that registers the custom post type:

// FLUSH REWRITE RULES ON ACTIVATION

function hbos_orders_rewrite_flush () {
	register_hbos_orders_cpt();
	flush_rewrite_rules();
}

register_activation_hook( __FILE__, 'hbos_orders_rewrite_flush' );

And that’s the basics for registering a custom post type. What you do with it beyond that is really up to you: I’d be interested to hear what interesting tasks you’re using them to do.

Categories: Code & Snippets | Permalink

What next?

Hire me

If you couldn't quite manage this yourself, find it too intimidating, or just don't have the time to do it, you can always hire Dave to do it. Please get in touch so that we can discuss your needs.

Leave a comment

If you have a question, update, or comment about the tutorial, please leave a comment. I try and respond to every comment, though it may take a few days, so please check back soon.

Keep your site backed up, updated & secure

I provide a service called The WP Butler, which helps you stay on top of the maintenance of your WordPress site. Instead of worrying about whether your site is secure, updated and backed up, The WP Butler handles all that for you on a regular basis, so that you can focus on doing what you do best. If you use coupon DIWW, you'll save 15% on our already-low-prices for all maintenance plans.

Author: Dave Clements

Dave Clements has been building websites for close to a decade and in 2010, he formalised that by starting his own company, The UK Edge. He now works on a variety of web projects, from simple tasks like installing a new WordPress site, to consulting on problems, or redesigning his clients' sites. He also runs Do It With WordPress, a site dedicated to providing free tutorials on WordPress. When he's not building your new website, you can find Dave eating Wheat Thins, spending time with friends and family, watching Indie films, fostering kittens from the local Humane Society, listening to some dubstep, dance and electronic rock, and exploring the world.

Leave a Reply