Create Custom Post Types in WordPress

This article will teach you how to implement WordPress custom post types. Knowing how to code WordPress custom post types from scratch opens the door to vast possibilities, not only for you as the developer but for your clients as well. WordPress has always been a powerful tool, but with this knowledge you can turn a basic blog website into a massive, multilayer powerhouse.

The Problem

I recently had a client come to us with a WordPress website that used it’s blog as a way to add and display animals that were up for adoption. They then add another WordPress site set up on a separate domain that had blog articles on it. When you clicked to read their blog, you would basically leave the main website and travel to another website. My goal in our redesign and restructuring would be to merge the two together. With WordPress custom post types I was able to do just that.

My vision was to have your regular ‘Posts’ link on the dashboard menu, then below it have a ‘Pet Posts’ link that would harness the same power the posts have but be completely separate from each other. Plus I wanted custom taxonomies instead of using the standard ‘Categories’ and ‘Tags’. This would allow our client to add blog posts and add pets on their main site and not have to log into a completely different dashboard to enter their blogs.

A Short Aside

My goal with this blog has always been to give other developers like me the knowledge that I am learning in my current projects. I am young and always learning new techniques. I am challenged on a daily basis and that helps me grow. So I google search a lot of issues I run into and read a lot of articles that are out of date or only give me pieces of the puzzle. I want my blog to show you things that are happening now and how to stay ahead with your development projects. When I struggle to find what I am looking for online, I know that when I finally figure it out I need to share it so that you guys and gals can have a resource.

The Solution

I have to start my solution by giving you a wonderful resource. There is a site called GenerateWP and it gives you a gui or interface that helps you build many different things in WordPress. You may think this is a cheaters way of generating code, but if you don’t know anything about making WordPress custom post types, this will help you gain your sea legs. Enter random things in the fields and then add it into your website’s functions.php file and see the results. The results will teach you what is actually going on in the code. This is the only way you are going to learn and be able to no longer rely on a generator.

Here is the code you would want to add into your functions.php file to create a custom post type:

// Register Custom Post Type
function custom_post_type() {

	$labels = array(
		'name'                => _x( 'Post Types', 'Post Type General Name', 'text_domain' ),
		'singular_name'       => _x( 'Post Type', 'Post Type Singular Name', 'text_domain' ),
		'menu_name'           => __( 'Post Type', 'text_domain' ),
		'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
		'all_items'           => __( 'All Items', 'text_domain' ),
		'view_item'           => __( 'View Item', 'text_domain' ),
		'add_new_item'        => __( 'Add New Item', 'text_domain' ),
		'add_new'             => __( 'Add New', 'text_domain' ),
		'edit_item'           => __( 'Edit Item', 'text_domain' ),
		'update_item'         => __( 'Update Item', 'text_domain' ),
		'search_items'        => __( 'Search Item', 'text_domain' ),
		'not_found'           => __( 'Not found', 'text_domain' ),
		'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
	);
	$args = array(
		'label'               => __( 'post_type', 'text_domain' ),
		'description'         => __( 'Post Type Description', 'text_domain' ),
		'labels'              => $labels,
		'supports'            => array( ),
		'taxonomies'          => array( 'category', 'post_tag' ),
		'hierarchical'        => false,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => true,
		'show_in_admin_bar'   => true,
		'menu_position'       => 5,
		'can_export'          => true,
		'has_archive'         => true,
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'capability_type'     => 'page',
	);
	register_post_type( 'post_type', $args );

}

// Hook into the 'init' action
add_action( 'init', 'custom_post_type', 0 );

Wow! That is a bunch of gibberish isn’t it! Well, let’s break it down so you know what to manipulate for your project.

The first part is declaring the function or giving our function a name. Where is says custom_post_type() you will simply replace that with the name you want to give it. Try to be specific with the names that you give the function. If you are using a child theme or if you have a lot of plugins installed, you don’t want to name it something that might already be used. Some developers recommend adding your initials to the beginning of the name just to make sure there would be no conflict.

function custom_post_type() {

Next we have the declaration of the labels. All we are doing here is creating an array and setting it equal to a variable called labels. Within this array we are telling WordPress what should be used in the dashboard areas of this new custom post type. The first item in the array is the name. Here we would tell WordPress what this custom post type is. For my client I used ‘Pet Posts’. Notice we are using uppercase letters in the beginning of each word because these are basically labels on your dashboard. The next field in the array is singular name. This would be ‘Pet Post’ or the singular version of your title. The menu name is very important because it is what you will read on the left side menu on the dashboard. Play around with these labels and then go into your dashboard and see what is changing.

// Register Custom Post Type
function custom_post_type() {

	$labels = array(
		'name'                => _x( 'Post Types', 'Post Type General Name', 'text_domain' ),
		'singular_name'       => _x( 'Post Type', 'Post Type Singular Name', 'text_domain' ),
		'menu_name'           => __( 'Post Type', 'text_domain' ),
		'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
		'all_items'           => __( 'All Items', 'text_domain' ),
		'view_item'           => __( 'View Item', 'text_domain' ),
		'add_new_item'        => __( 'Add New Item', 'text_domain' ),
		'add_new'             => __( 'Add New', 'text_domain' ),
		'edit_item'           => __( 'Edit Item', 'text_domain' ),
		'update_item'         => __( 'Update Item', 'text_domain' ),
		'search_items'        => __( 'Search Item', 'text_domain' ),
		'not_found'           => __( 'Not found', 'text_domain' ),
		'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
	);
	

Next we have the declaration of the arguments. Again we have an array and we are setting it equal to a variable called args. $args is a very common variable in WordPress and you can see it in use in the Loop when you query a post most commonly. The first item in the array is very important. the ‘label’, not to be confused with the labels we just created, is the custom post type. For my client I used ‘pets’. The standard post type in WordPress is ‘post’. You can see this if you go into your dashboard, click on Posts, then click Categories, then hover over one of the categories that are listed there and you will see at the bottom left hand corner of your browser window a tooltip type box will appear that gives you information about that category. The last time in the pop up says post_type=’post’. Ours will read post_type=’pets’.

Don’t worry about description unless you want to add one in and see what it does. Then you will see ‘labels’ again where we have now used the variable $labels to call in the array we create above. I’m not going to go into every item that is in this array but there are a few that are pretty important. The first being ‘supports’ which takes an array of items. This array tells WordPress what standard items you want on your new custom post type’s page. Do you want the standard title field, or the standard editor to add your text, or do you want the excerpt field? This is where you would enter the values into the array. Go back into GenerateWp and on the interface choose the ‘options’ tab where you will see a group of checkboxes. Choose a couple of them and then regenerate your code. Put your new code back into your functions.php file and then go into your new custom post type’s page and see what appears. It will all make sense then! Next we need to talk about ‘taxonomies’. In this array you can either use the standard categories and tags or you can create custom taxonomies. In my next article I will show you how to create custom taxonomies to help you extend your WordPress custom post types. Lastly, you must register your new post type which is the last piece of code below. Make sure you change ‘post_type’ with what you entered into the ‘label’ field.

	$args = array(
		'label'               => __( 'post_type', 'text_domain' ),
		'description'         => __( 'Post Type Description', 'text_domain' ),
		'labels'              => $labels,
		'supports'            => array( ),
		'taxonomies'          => array( 'category', 'post_tag' ),
		'hierarchical'        => false,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => true,
		'show_in_admin_bar'   => true,
		'menu_position'       => 5,
		'can_export'          => true,
		'has_archive'         => true,
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'capability_type'     => 'page',
	);
	register_post_type( 'post_type', $args );

None of this will work if you don’t add this action into the init function by using the code below. Enter the name of your function where it says ‘custom_post_type’.

add_action( 'init', 'custom_post_type', 0 );

You Got This!

With the ability to create WordPress custom post types, you have just doubled the power of your WordPress site. Adding this knowledge to your tool belt, along with the ability to create custom taxonomies, will make you feel so much more confident when you are faced with larger clients with larger websites. This means larger budgets people!