How to Create a Custom Post Type in WordPress

Posted OnFeb 3, 2014     CategoryDevelopment     Comments2 comments

In order to do this, you need access to your WordPress files and the WordPress Dashboard. For this tutorial, we’re going to be writing our code into functions.php, which will make the custom post type theme specific, so for future use it’s worthwhile creating a plugin if you are comfortable in doing so.

We strongly advise that you create a child theme for making these changes as well.

Step 1) Connect to your server and browse to your theme’s folder (wp-content/themes/ThemeName)

Step 2) Locate and open functions.php. If you’re using a child theme and this does not exist, please create it.

Step 3) We need to create a function to house the details of our custom post type, let’s break it down.

function custom_post_recipe() {

$labels = array(
	'name'               => _x( 'Recipe', 'post type general name' ),
	'singular_name'      => _x( 'Recipe', 'post type singular name' ),
	'add_new'            => _x( 'Add New', 'Recipe' ),
	'add_new_item'       => __( 'Add New Recipe' ),
	'edit_item'          => __( 'Edit Recipe' ),
	'new_item'           => __( 'New Recipe' ),
	'all_items'          => __( 'All Recipes' ),
	'view_item'          => __( 'View Recipes' ),
	'search_items'       => __( 'Search Recipes' ),
	'not_found'          => __( 'No recipes found' ),
	'not_found_in_trash' => __( 'No recipes found in the Trash' ), 
	'parent_item_colon'  => '',
	'menu_name'          => 'Recipes'
	);

Here we’ve created our function called custom_post_recipe, and immediately defined a variable called $labels which we’re using to store an array of information for the custom post type. Most of this reads like plain English so we wont go through it at the moment.

$supports = array( 
	'title'
	'editor', 
	'author',
	'custom-fields',
	'post-formats',
	'comments'
	'revisions'
	); 

Just below $labels we define another variable which stores yet another array of data for our custom post type, in this case we’re telling WordPress what functionalities we want the custom post type to be able to have. Title and editor meant that you can create the title and are able to write the post with the editor, for instance. Not defining values will disable their functionality, so if you didn’t want revisions, you can just delete that line.

$details = array(
	'labels'        => $labels,
	'description'   => 'Everything you want to know about a recipe!',
	'public'        => true,
	'menu_position' => 5,
	'supports'      => $supports,
	'has_archive'   => true,
	)

Now we have another variable, our third and final, this time called $details. This calls in our other variables to construct the full specifications of the custom post type, including some extras, such as the description, menu position (for in the Dashboard), that the posts are public, and have archive pages. Now it’s time to wrap it up.

register_post_type( 'recipe', $details ); 
	}
add_action( 'init', 'custom_post_recipe' );

We use the WordPress function register_post_type with two arguments, the first is the name of the post type we want to register, the second is the $details variable, which pulls in all the required data, thereafter we close the function off.

Finally we use add_action with two arguments, the first is when we want to have the post type registered and we specify at initialization, note that it must be at this time or it will not work. The second argument is the name of the function we created earlier, so WordPress knows what it needs to do when taking care of it.

When you’re done here, save your changes.

Step 4) Head into the WordPress Dashboard to see your work come to life.

You should see something like this, with the Recipes menu item on the left below Posts, and if you hit add new, you’ll have the regular post editing interface.

add new recipe

For your review, here’s the entirety of the code we’ve created.

function custom_post_recipe() {

$labels = array(
	'name'               => _x( 'Recipe', 'post type general name' ),
	'singular_name'      => _x( 'Recipe', 'post type singular name' ),
	'add_new'            => _x( 'Add New', 'Recipe' ),
	'add_new_item'       => __( 'Add New Recipe' ),
	'edit_item'          => __( 'Edit Recipe' ),
	'new_item'           => __( 'New Recipe' ),
	'all_items'          => __( 'All Recipes' ),
	'view_item'          => __( 'View Recipe' ),
	'search_items'       => __( 'Search Recipes' ),
	'not_found'          => __( 'No recipes found' ),
	'not_found_in_trash' => __( 'No products found in the Trash' ), 
	'parent_item_colon'  => '',
	'menu_name'          => 'Recipes'
	);

$supports = array( 
	'title'
	'editor', 
	'author',
	'custom-fields',
	'post-formats',
	'comments'
	'revisions'
	); 

$details = array(
	'labels'        => $labels,
	'description'   => 'Everything you want to know about a recipe!',
	'public'        => true,
	'menu_position' => 5,
	'supports'      => $supports,
	'has_archive'   => true,
	);

register_post_type( 'recipe', $details ); 
	}
add_action( 'init', 'custom_post_recipe' );

2 comments

Comments

  1. Lanre

    Thanks so much. I successfully created my post type and its taxonomies, a new role and capabilities. However, I need the new custom users to manage just the custom post posted by them.

    1. Aaron Osteraas

      I think the easiest way to do that would be using a plugin called User Role Editor, where you can change the permissions per author, shouldn’t be too hard 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Send this to friend