Custom Post Type

Function For Post Type Labels

WordPress has the ability to have different types of post types, the main post type you will use in WordPress is the post type called post. But there are other defaults such as Pages and Attachments. All the post types in WordPress are stored in the same table wp_posts there is a column on this table which will decide the type of post, this is defined as post_type.

In version 3.0 of WordPress a function was created to allow you to easily create your own post types.

To create custom post types you need to use the function register_post_type().

add_action( 'init', 'create_post_type' );

function create_post_type() 
    $args = array();
    register_post_type( 'post_type_name', $args);

One of the parameters is $args which you need to send in a number of parameters to create the post type. One of these parameters is all the labels that are used on the post type for menu or creating new post types.

$labels = array( 
  	'name'               => __( 'plural post type name', 'text_domain' ),
  	'singular_name'      => __( 'single post type name', 'text_domain' ),
  	'add_new'            => _x( 'Add New single post type name', '${4:Name}', 'text_domain' ),
  	'add_new_item'       => __( 'Add New single post type name', 'text_domain}' ),
  	'edit_item'          => __( 'Edit single post type name', 'text_domain' ),
  	'new_item'           => __( 'New single post type name', 'text_domain' ),
  	'view_item'          => __( 'View single post type name', 'text_domain' ),
  	'search_items'       => __( 'Search plural post type name', 'text_domain' ),
  	'not_found'          => __( 'No plural post type name found', 'text_domain' ),
  	'not_found_in_trash' => __( 'No plural post type name found in Trash', 'text_domain' ),
  	'parent_item_colon'  => __( 'Parent single post type name:', 'text_domain' ),
  	'menu_name'          => __( 'plural post type name found', 'text_domain' ),

For more information on create post types you can view a previous tutorial I wrote which goes into more detail.

Creating Custom Post Types

If you are going to create lots of different post types then you will have a lot of similar code for creating the labels of the post type. Here is a handy little function to create the labels on your post types, by passing in the singular and plural labels.

function create_post_type_labels($singular, $plural = null) {

	if ($plural === null) {
		$plural = $singular.'s';

	$labels = array(
		'name'               => __( $plural, 'text-domain'),
		'singular_name'      => __( $singular, 'text-domain'),
		'menu_name'          => __( $plural, 'text-domain'),
		'name_admin_bar'     => __( $singular, 'text-domain'),
		'add_new'            => __( 'Add New '.$singular, 'text-domain'),
		'add_new_item'       => __( 'Add New '.$singular, 'text-domain'),
		'new_item'           => __( 'New '.$singular, 'text-domain'),
		'edit_item'          => __( 'Edit '.$singular, 'text-domain'),
		'view_item'          => __( 'View '.$singular, 'text-domain'),
		'all_items'          => __( 'All '.$plural, 'text-domain'),
		'search_items'       => __( 'Search '.$plural, 'text-domain'),
		'parent_item_colon'  => __( 'Parent '.$plural.':', 'text-domain'),
		'not_found'          => __( 'No '.$plural.' found.', 'text-domain'),
		'not_found_in_trash' => __( 'No '.$plural.' found in Trash.', 'text-domain')

	return $labels;
Back to top

Learn how to code with Treehouse

  • Learn projects with access to 1000+ videos
  • Practice live with our Code Challenge Engine
  • Get help in our members-only forums

Start with a 7 day free trial