Creating a Custom Post Type

WordPress has been around for several years, been used by millions of people, and installed on millions of websites. It’s pretty good at being everything to everyone out of the box. But sometimes you have to take a few extra steps to make it work for you. Custom Post Types are a good example of this.

WordPress comes packaged with the ability to create pages and posts. Sometimes that’s just not enough. Usually you can find a plugin that will add the functionality to add different kinds of content:

  • WooCommerce
  • Easy Testimonials
  • WP Job Manager

All of these plugins create new post types and ways of displaying those post types. But they also come with a lot of overhead that maybe you don’t need. Maybe you just want to add products for affiliate purposes: a description, image, and external URL for your affiliate link. Do you really need to install a monster plugin like WooCommerce to handle something that simple? Definitely not.

What if the custom post type you want to create isn’t created by a plugin? Maybe you have a science fiction website and you want to create an online documentation of alien species. Try to find that plugin in the WordPress directory.

The point is, there are times you might want to create a custom post type yourself vs relying on a plugin. If that’s you, I have great news. IT’S REALLY EASY!! Especially since I am providing all of the code and commented it for you to understand what’s happening!

For this tutorial, I’m going to create a new post type Recipes with recipe categories called Types and with the ability to add tags. I’m going to put all of the code inside a plugin. Why? It just makes life easier. If you can create a website with WordPress, you can create a custom post type plugin for that website. If you really don’t want to deal with a plugin, you can copy everything from the code below into your functions.php file minus lines 2-10 and lines 125-139. On a side note, that should tell you how easy creating a WordPress plugin really is.

I am also making this plugin available for FREE on my Divi Plugins site. Two versions are available: the Divi version adds the Divi Page Builder to your CPT and the other is the standard version and will work on any WordPress theme.

Download Now

If you want to create the plugin yourself, here’s the full code and then I’ll give you a few tips:

Update: Elegant Themes shared a bit of handy code that will allow you to use the Divi Page Builder (Divi theme or plugin required) when creating any CPT. Below is the code you would add to the file above to allow the use of the page builder on our recipes. Again, this only applies if you are using the Divi theme or plugin.

Replace ‘recipes’ in the code above with your custom post type. You can also enable the page builder on multiple post types within the same function like this:

By default, the layout created from the page builder in your new CPT will be displayed in a template with a sidebar. And the content will not take up the entire width of the container. To fix this, copy the single-project.php template from the Divi folder into your child theme folder. Then rename it single-YOUR-POST-TYPE.php. This will not only fix the content not stretching all the way but will also remove the sidebar and give you a fullwidth page to work with.

Now back to explaining the plugin:

Before you get too overwhelmed with the code above, you can easily make this code work for your custom post type by simply replacing the word “recipe” and “recipes” with any other word and voila!! You have a [insert your word] custom post type. It really is that easy. Same thing with the Type categories. Replace “type” and “types” with your word (solar system/solar systems, planet/planets, galaxy/galaxies).

You can see in the code there are four functions (feel free to rename this however you like). The first three register the tags, categories, and post type respectively. The last function calls all three functions followed by a flush rewrite. This is the same as going to your permalinks and clicking on the save button. If you didn’t do this, the pretty URLs for your new post types wouldn’t work right until you manually updated your permalinks.

If you decide you don’t want to use categories or tags, you can delete those functions (the first two) and also delete them from the last function just before the flush rewrite.

Now we need to display our new custom post types. If you’re using Divi, you can display them with Porfolio Posts Pro. With this plugin, you can display any custom post type in a grid, carousel or fullwidth view. If you don’t have Divi or the plugin, you’ll have to rely on your theme’s template files to display your new posts.

By default, your theme will display your new custom post types using your archive.php and single.php files. If you want to modify these templates just for the new CPTs, you’ll need to create a template for them. Copy the archive.php file to your child theme folder. Rename it archive-[enter your post type].php. In the case of the example, it’s recipes. So we’ll rename the template archive-recipes.php.

The same applies to individual posts. We’ll copy the single-project.php file from our parent theme and paste it into our child theme folder. Then we’ll rename it single-recipes.php.

That’s it. You’ve now created a new custom post type. You can create new post types within the same plugin by copying the first three functions, changing the names, and then adding them to the dp_recipe_cpt_install function. Good luck!

    3 Comments

  1. Hi! Perhaps I haven’t entirely understood your explanation, but I’m currently trying to introduce a custom post type, so that I can on a separate page list articles underneath. Since the page is also built with the divi builder, I need the builder to give me the option to select my customs post type (let’s called it recipes like you did), otherwise I have no way of displaying the entirety of my recipes on the overarching page with the builder. I copied all of the code and changed the corresponding parts in the code to my personal custom post, however it’s still not giving me an option to display the new type. What could I do to change that? Thanks!

    Anna Finke

    March 10, 2017

  2. Agreed Anna, this is the only reason we still use Visual Composer.

    Lawrence Weiss

    April 28, 2017

  3. How would you integrate schema.org for the recipes, Lawrence?
    I feel like this would be the missing piece of the puzzle 😀

    Manuela

    July 9, 2017

Post a Reply

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