How to create Custom Post Types in WordPress

I’ve been working on a game database that’s powered by a vanilla WordPress instance. For the project it made sense to create a new post type (game), as well as a custom taxonomy (platform). I didn’t want to use a plugin and instead opted to create the new post type as part of the child theme’s function.php file.

Here’s how I did it.

The Custom Post Type (Game)

Let’s take a look at the code for the custom post type first. We’ll assume we’re going to use the built-in taxonomies for now (Tags and Categories), so that it works just like a regular post. The only difference is that we’ll have a new menu item just for Games in the admin menu, like this:

The code to make that happen looks like this:

This function creates the Game post type, gives it the relevant labels (for the admin menu), and adds both default WordPress taxonomies to it (namely categories and tags). This happens in the last step before the function is called via the init hook. There are plenty more parameters you can set, as outlined in the documentation.

The Custom Taxonomy (Platform)

I thought it might be nice to a have a completely different taxonomy system in my game database, namely one that would allow me to categories my games by platform. This would be something like PS3, PS4, GameCube or PC. Ideally this taxonomy would work like the regular categories and allow parenting as well, so we can have a marketplace like Steam or GOG parented to the PC Platform.

The following code makes that happen:

This looks a little more complex, only because we have to setup all the labels for the admin area. It’ll look just like the one that lets us amend the default Categories (under Posts), except now it’ll be a whole new menu (Platform) available under Games. The “textdomain” placeholder above is for potential translations.

All these and other parameters are explained in the documentation. There’s one undocumented peculiarity that I’ve noticed: the selection box for taxonomies does not show up, unless we set the “show_in_rest” parameter to true. When left to false, those boxes on the right hand side when you write a custom post might not reveal themselves. Go figure.

Adding Platform to Games

Once created, we need to add our custom taxonomy to our custom post type. We’ve already done this above with the regular default taxonomies, which we can now remove (or leave in place, depending on our needs). We do that with the register_taxonomy_for_object_type() function, which is documented here.

The last line in this code will take care of it (we’ve seen the other two lines in the register_post_type() function above):

And that’s it! Some would argue it’s super easy, but as it is with many such things, I was missing several key pieces of the puzzle to make this work. I hope this guide will help get your custom post type projects off the ground.

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs several YouTube channels and websites, and he's also live on Twitch sometimes. To support him on his mission to make the world a better place, you can support him on Patreon or buy him a coffee.

Add your voice!