Looking for a similar platform? We’d love to hear from you!

  Take a Survey
Tutorials, WordPress

How To Create WordPress Custom Post Type

by Patryk Gąsior

This article will give you a closer look at the WordPress Custom Post Type. 

What is WordPress Custom Post Type?

By default, WordPress offers us two types of content: “Page” and “Post.” But what if you need another, like “Product,” “Event,” “Movies,” etc.? In those cases, you can rely on Custom Post Types. They offer the possibility of creating our own content types and therefore extending WordPress's functionality.

You can locate your Custom Post Types in the navigation bar.

How Do Custom Post Types Work?

WordPress Custom Post Type works like a regular post type. Therefore it can have a taxonomy like a category or a tag or something completely different that still has the same functionality as these.

For example:

Custom Post Type called “Movie” has a custom taxonomy called “Genre,” which acts exactly the same way as Post’s Category would, with some minor exceptions (mostly code-side: some functions are designed strictly for categories, so they wouldn't work as intended in custom taxonomy; that is something that any programmer should keep in mind). Custom taxonomies and Custom Post Types feature other similarities to default content types, such as Category/Archive pages, thumbnail, etc.

Each Custom Post Type follows with custom taxonomies found after clicking on a specific CPT.

How To Implement Custom Post Types Into My WordPress Site?

There are two main ways of implementing Custom Post Types into WordPress. The easiest one, especially for the non-coding user, is using a plugin. Don't be fooled, though - this method carries a significant flaw in the overall WordPress's structure. While all plugins, including the CPT ones, offer quick solutions to our struggles, they usually feature many other functionalities that we probably will never benefit from. And, as we know, overloading our website with irrelevant code can affect our website performance.

The second, more reliable method is to code the WordPress Custom Post Type manually inside a theme or a custom plugin. You can either hire a developer to handle this for you or do it yourself (not recommended if you're not 100% sure of what you're doing - every modification comes with the risk of breaking an entire website, so be careful and always do a backup before diving in). This way is much more optimized for your needs, as it won't introduce any useless code to your website while still solving all the problems you're facing.

Installing Custom Post Types With a Plugin

Let's see how to install a Custom Post Type using a plugin. I highly recommend doing some research before choosing the right tool. Make sure you know at least a few opinions on a given plugin and the estimated time between updates. In this case, I've decided to use Custom Post Type UI.

Once the plugin is installed, we're ready to set up our Custom Post Types. At first glance, we can see that this plugin features creating custom taxonomies as well:

Custom Post Type plugins give you the option to add and edit post types and taxonomies.

After clicking Add/Edit Post Types, we can see a large menu of options to fill in: labels (those are used in WP-Admin Dashboard in places like “Add new X” “Search X,” etc. ), settings (those decide if our post type supports certain options, like archive page, hierarchy, categories, tags, etc.). Once these are set up, we can go ahead with using our new Custom Post Type. It'll share front-end templates with the regular post types.

Installing Custom Post Types With Custom Code

And how do we prepare a custom code that will create a Custom Post Type with a Custom taxonomy? Let me show you below. What's more, I'll mention some interesting possibilities that you won't be able to achieve with the other method.

Here we have a basic code that creates a simple Custom Post Type "Movies," with extra support for custom taxonomy "Genre":

function createPostType() {
 
	register_post_type( 'movies',
    	array(
        	'labels' => array(
            	'name' => __( 'Movies' ),
            	'singular_name' => __( 'Movie' )
        	),
   	        'taxonomies'  => array( 'genre' ),
        	'public' => true,
        	'has_archive' => true,
        	'rewrite' => array('slug' => 'movies'),
        	'show_in_rest' => true,
 
    	)
	);
    
}
add_action( 'init', 'createPostType');

Next, I'll create that "Genre" taxonomy I've already created support for (if you won't use a custom taxonomy, you can remove the ‘taxonomies’ from CPT options ):

function createCustomTaxonomy() {
 
  $genreLabels = array(
	'name' => _x( 'Genres', 'taxonomy general name' ),
	'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
	'search_items' =>  __( 'Search Genres' ),
	'all_items' => __( 'All Genres' ),
	'parent_item' => __( 'Parent Genre' ),
	'parent_item_colon' => __( 'Parent Genre:' ),
	'edit_item' => __( 'Edit Genre' ),
	'update_item' => __( 'Update Genre' ),
	'add_new_item' => __( 'Add New Genre' ),
	'new_item_name' => __( 'New Genre Name' ),
	'menu_name' => __( 'Genres' ),
  );     
 
  register_taxonomy('genre',array('movie'), array(
	'hierarchical' => true,
	'labels' => $genreLabels,
	'show_ui' => true,
	'show_admin_column' => true,
	'query_var' => true,
	'rewrite' => array( 'slug' => 'genre' ),
  ));
}
add_action( 'init', 'createCustomTaxonomy', 0 );

If you'd like to learn more about settings and labels, follow here for full information.

This method also lets us set up our custom type template. Default templates are called “single.php,” “page.php,” but we can also create a custom template based on a default one. To do that, just copy “single.php,” rename it to “single-movie.php,” and alter the code to your liking. You can read more about this method here.

WordPress Custom Post Type Done The Right Way

As you can see, the custom way of setting up a Custom Post Type for WordPress is more complex but can bring you more benefits. Limiting extra code to the absolute necessity will result in a faster website; plus, remember that trusting a third-party plugin is always a risk, no matter how many enthusiastic reviews you read.

That's why, if you have room in your budget or feel fearless about coding, go for a custom solution. It'll result in a quality product that'll bring diversity to your website and streamline your content input.

What's your experience with Custom Post Types? If you have any trouble with implementing them yourself, let me know below - WP Kraken team will be happy to help!

Patryk Gąsior

Disqus Comments

Reading time 5 minutes

This website uses cookies to ensure you get the best experience. More info I agree