I am interested in
How to create custom post types in wordpress
December 28, 2018
December 28, 2018

When Posts and Pages are not enough. Creating Custom Post Types in WordPress.

In this tutorial, we are going to edit functions.php file. Don’t know where to find this file? No worries, we have an entire article where we teach you where to find it. Also, you can watch a video tutorial on our channel.

By default, there are two custom post types in WordPress – posts and pages. While posts are meant for articles as blog posts, pages are meant for a static content such as your Contact page or About us page for example.

But what if that’s not enough? What if you want to add another type? Let’s say you run a website and would like to have – instead of just Posts and Pages – tabs like Cars, Trucks and Motorcycles?

What we are going to do is that we are going to create three custom post types. Also, we are going to create a custom taxonomy called Brand. Basically every car, motorcycle and truck will be categorized by this brand. For example, we are going to have a car (custom post type) a brand Chevrolet (taxonomy) and a car post Chevrolet Malibu.

Open your functions.php file and add this snippet of code:


function create_post_type() {

  register_post_type( 'cars',
    array(
      'labels' => array(
        'name' => _x('Cars', 'post type general name'),
        'singular_name' => _x('Car', 'post type singular name'),
        'add_new' => _x('Add a new car', 'work item'),
        'add_new_item' => __('Add a new car'),
        'edit_item' => __('Edit car'),
        'new_item' => __('New car'),
        'view_item' => __('View car'),
        'search_items' => __('Search cars'),
        'not_found' => __('Nothing found.'),
        'not_found_in_trash' => __('Nothing found in the trash.'),
        'parent_item_colon' => ''
      ),
      'public' => true,
      'has_archive' => true,
      'supports' => array( 'title', 'editor', 'thumbnail' ),
			'rewrite'  => array('slug' => 'cars'),
    )
  );

	register_post_type( 'trucks',
    array(
      'labels' => array(
        'name' => _x('Trucks', 'post type general name'),
        'singular_name' => _x('Truck', 'post type singular name'),
        'add_new' => _x('Add a new truck', 'work item'),
        'add_new_item' => __('Add a new truck'),
        'edit_item' => __('Edit truck'),
        'new_item' => __('New truck'),
        'view_item' => __('View truck'),
        'search_items' => __('Search trucks'),
        'not_found' => __('Nothing found.'),
        'not_found_in_trash' => __('Nothing found in the trash.'),
        'parent_item_colon' => ''
      ),
      'public' => true,
      'has_archive' => true,
      'supports' => array( 'title', 'editor', 'thumbnail' ),
			'rewrite'  => array('slug' => 'trucks'),
    )
  );

	register_post_type( 'moto',
    array(
      'labels' => array(
        'name' => _x('Motorcycles', 'post type general name'),
        'singular_name' => _x('Motorcycle', 'post type singular name'),
        'add_new' => _x('Add a new motorcycle', 'work item'),
        'add_new_item' => __('Add a new motorcycle'),
        'edit_item' => __('Edit motorcycle'),
        'new_item' => __('New motorcycle'),
        'view_item' => __('View motorcycle'),
        'search_items' => __('Search motorcycles'),
        'not_found' => __('Nothing found.'),
        'not_found_in_trash' => __('Nothing found in the trash.'),
        'parent_item_colon' => ''
      ),
      'public' => true,
      'has_archive' => true,
      'supports' => array( 'title', 'editor', 'thumbnail' ),
			'rewrite'  => array('slug' => 'motorcycles'),
    )
  );

register_taxonomy( 'brand', array( 'cars', 'trucks', 'motorcycles' ), array(
				'hierarchical' => true,
				'label' => 'Brand'
			)
		);
}

add_action( 'init', 'create_post_type' );

After you save the functions.php, new tabs will appear in your WordPress menu just like on the picture below:

custom post types in wordpress

Once you click one the custom post types (Cars, Trucks or Motorcycles) you will be able to add a new item.

custom post types cars

Such items can be further categorized by brands. Creating such brand is as easy as creating a blog category:

custom taxonomy wordpress

Sign up to our newsletter