Tuesday , 16 July 2019
Home / Programming / WordPress / Plugins / WordPress Custom Post Types – WordPress Plugin Tutorial
WordPress Custom Post Types
WordPress Custom Post Types

WordPress Custom Post Types – WordPress Plugin Tutorial

This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

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.

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.

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.

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.

WordPress Custom Post Types
WordPress Custom Post Types

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.

About Jasko Koyn

Check Also

WordPress Widgets API

Widgets API – WordPress Plugin Tutorial

The widgets API is what we’ll be learning next. It’s very simple to use. It …

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.