One of the amazing features that comes with WordPress is the ability to create WordPress custom post types. What are WordPress custom post types? A post type is a type of content. For example, a post is a post type. More information about post types can be found here. There will be times where you may want a custom post type. If you own a recipe site, you may want a recipe post type because a post won’t necessarily give you all the options you may need.
If you don’t understand so far, let’s create our own post type. This will be a 3 part tutorial. Creating WordPress custom post types is very easy. WordPress takes care of displaying, updating and managing your content. All we have to do is pass some options to WordPress to get started. This tutorial will depend heavily on arrays. If you’re a bit rusty on arrays, then go and refresh your memory on arrays by clicking here.
WordPress Custom Post Types
Let’s create a new plugin called wpCustomPostTypeEx and edit our index.php file to this.
* Plugin Name: WP Custom Post Type
* Plugin URI: http://jaskokoyn.com
* Description: A Basic WordPress Plugin to show how custom types work.
* Author: Jasko Koyn
* Version: 1.0
* Author URI: http://jaskokoyn.com
// Set up our WordPress Plugin
if ( version_compare( get_bloginfo( 'version' ), '3.1', '<' ) )
wp_die( "You must update WordPress to use this plugin!" );
register_activation_hook( __FILE__, 'jk_check_WP_ver' );
// Include or Require any files
include( 'inc/recipe-post-type.php' );
// Action & Filter Hooks
add_action( 'init', 'jk_create_recipe_post_type' );
We’re using the action hook init and our function jk_create_recipe_post() will be created in the recipe-post-type.php file. Let’s create that file now and insert this bit of code.
$labels = array (
'name' => 'Recipes',
'singular_name' => 'Recipe',
'add_new' => 'Add New Recipe',
'add_new_item' => 'Add New Recipe',
'edit' => 'Edit',
'edit_item' => 'Edit Recipe Review',
'new_item' => 'New Recipe',
'view' => 'View',
'view_item' => 'View Recipe',
'searh_items' => 'Search Recipes',
'not_found' => 'No Recipes found',
'not_found_in_trash' => 'No Recipes found in trash',
'parent' => 'Parent Recipe'
$args = array (
'labels' => $labels
register_post_type( 'recipes', $args );
We have 2 arrays here, but let’s focus on the register_post_type() function first. This function takes of creating our WordPress custom post types. It has 2 parameters. The first parameter is the name of your post type. This parameter is required. You can name it whatever you want, but it can only be 20 characters long and can not contain capital letters or spaces. In this tutorial, we name it recipes.
Our next parameter is optional,which contains a vast array of arguments. This is where we can customize our WordPress custom post types. As you can see, I created the array out of the function for readability. Our $args variable contains our array. The first item in our array contains labels. These labels are simply texts that appear in our custom post type and it’s pages.
We actually have to create another array for our labels because there’s multiple labels to define. Instead of creating the array inside of the array, we create it outside of the array and just point to it. We do this, for once again, readability.
Let’s take a look at our labels array. Let me go over each item in the array.
- name – A general name for this post type. It’s recommended you say it in plural form.
- singular_name – The name for this post type if there was only one.
- add_new – The text displayed when the user wants to add a new recipe.
- add_new_item – The text displayed for add new item
- edit – The text displayed for edit.
- edit_item – The text displayed for the edit text item.
- new_item – The text displayed for new item.
- view – The text displayed for item.
- view_item – The text displayed for view item.
- search_items – The text displayed for search item.
- not_found – The displayed for not found.
- not_found_item – The Text display for not found item
- parent – The text displayed for the parent.
Remember that these options are optional. There are more labels we can define, but these are the most important ones. If you don’t define a certain label, WordPress will define one for you. BUT WAIT, THERE’S MORE!
Let’s update our $args array to this.
$args = array (
'labels' => $labels,
'query_var' => 'recipes',
'rewrite' => array( 'slug' => 'recipes/' ),
'public' => true,
'menu_position' => 15
//'menu_icon' => LINK TO YOUR ICON IMAGE
We’ve added some new values. Let me explain each one by one.
- query_var – Allows us to query this post type by using a string of our choice. In our case, recipes.
- rewrite – If the user has rewrite turned on. Then, our post type slug would be recipes/. Notice, I don’t create an array outside of this array. It’s because it’s only 1 item and there’s no need to create an array for every item.
- public – Determines whether or not this post type is accessible in the front end as well as the back end. You can set this to true or false. In our case, we want our visitors to be able to access this post type.
- menu_position – Determines where this will appear in the item. Can be any number from 1 – 100.
I’ve also added, but commented out the menu_icon item. We won’t be using this, but if you need it, you use that to define your own custom menu icon.
You should be able to activate your plugin and see your custom post type active in the admin dashboard.
You can fully use this custom post type now. You can create a new recipe, delete the recipe, and edit the recipe. Creating WordPress custom post types is easy. WordPress takes care a majority of the work. Though, the point of WordPress custom post types is to customize how you publish and control content. The only reason you should need a custom post type is when you’re finding that creating posts doesn’t allow you the flexibility you need.
We’ll continue on with WordPress custom post types by discussing meta data and taxonomy. We’ll be further extending this plugin in the next tutorial.