custom post typesWordPress use as content management system is growing rapidly and it is not about to stop. As a matter of fact WordPress is better as a content management system than any other free CMS I have used before. One feature of WordPress that makes WordPress good as a CMS is its malleability, making it suitable for a wide range of web development projects.

I have previously used WordPress to build large eCommerce websites, used it for school websites, used it to build review sites and recently created a hotel booking engine that runs on WordPress. All these projects required different approach since they have different features and each project’s needs are uniquely different from the other.

WordPress proved to be the most reliable  free CMS for these projects. Most importantly,WordPress custom post types play a pivotal role in creating  innovative solutions. They are the engine of innovation in WordPress; in this tutorial I will outlined how to harness the power of custom post types to build custom CMS in WordPress.

Introduction to Custom Post Types

When using WordPress to create a content management system, you will be required to display different types of content that the default posts and pages cannot display. When publishing content on WordPress it is referred to as post or page. The distinction between the two is the fact that posts are based on a timeline while pages can be static. WordPress post types are usually stored in the same database table - wp_posts they are however differentiated in the database by the column named - post_type. See the image below;

Custom Post Types

As you can see the post_type is a column in WordPress database that categorizes pages and posts. You can access your WordPress database by going to Phpmyadmin then choose the database on the left.

Hint: If you have multiple databases on your panel you can identify the specific database by looking for the ‘database name’ in the wp-config.php file.

Default WordPress post_types

WordPress has five default post_types that come with default WordPress installation, the include;

Post (Post Type: 'post')

Page (Post Type: 'page')

Attachment (Post Type: 'attachment')

Revision (Post Type: 'revision')

Navigation menu (Post Type: 'nav_menu_item')

If you have used WordPress you have probably created a post, created a page or added a menu in your blog. These are the default posts that you use for publishing content on WordPress. These default post_types are limiting especially when you are creating custom web project. If you want to use WordPress a content management system, you may use plugins to add features to your site.

Nevertheless there may be few plugins available for your precise needs. Using several plugins also comes with a downside; these plugins not only bloat your site but also make you reliant on third party developers who cannot always be trusted. Custom post types therefore provide a perfect solution for you to expand WordPress functionality and feel in control of your own destiny.

Custom Post Types

Custom post types allow you to create your own type of content other than the default 5 post types. This means you can create additional post type and use it to publish your content. This allows WordPress to be used for a wide range of content management. Example; you can use WordPress to build eCommerce since you can create custom post type for products. The possibilities of using custom post type are limitless since you can build any kind of website utilizing the custom post type feature.

Creating Custom Post Types

To begin creating custom post types you need to decide whether you will add them on the existing WordPress theme or you will create a plugin. I want to focus this tutorial on a recent project where a client requested my help in creating a WordPress site for listing city hotels.

I will show you using this example, how to use custom post types to create this kind of CMS. I have already set up  my localhost WordPress development environment. I previously setup WordPress on localhost –Xampp and have also installed evolve theme for tutorial demonstration. You can download evolve multipurpose responsive theme for FREE  here and let us get started.

evolve theme demo site

Creating custom post types will require us to create the post types, taxonomies and Meta boxes. Let’s look in depth at each of these steps.

Step 1: Registering Custom Post Types

The first step is register custom post type; we will be creating the custom post type on Evolve theme. You can also create a plugin for the custom posts but adding theme on the theme makes it quick and easier. We will be adding the code in the function.php file. To register custom posts we need to use the register_post_type() function as follows;

We will now be creating two arrays that store the values of the custom post type and also control how the custom post work.

Step 2: Adding Labels Array, Adding Values to Args Array

These two arrays are going to determine how the custom post types work and how and where they appear. The labels array contains the labels that define options for the custom array. The labels array should look like this:

The args array has a description that shows what the custom post does, it also has the menu position whose array key defines the position where the menu of this custom post appears in the dashboard menu and it is defined as 3. The array also has the support and archives that both define how to custom post works and its features. The args array also calls the preceding labels array, so it should look like this:

You should now add both arrays to the custom post registration and then add this code to function.php file. Here is the full code combine; check out the comments I have added to the code to differentiate each of the coded block discussed above:

You can now paste this code in your function.php; when adding this code ensure you are careful to avoid causing a parse error and a possible WordPress white screen of death.

functions hotels custom post types

If you have done everything correctly you should now see the hotels section listed in the WordPress dashboard menu. You should now click on it and try to add a hotel and see how it works. Here is how it should look:

How to Create Custom Post Types

Step 3: Creating Custom Taxonomies

The default WordPress posts use categories and tags to organize content. For custom post types we can create a different content structure that is more specific for our site. Creating custom taxonomies allows us to have categories for our custom post that are different from the default WordPress categories. To create them we should register custom taxonomies using register_taxonomy(); function:

We now need to add an array that contains labels which define the custom taxonomies and also example the args array to call the labels array as well as define the Boolean value for the hierarchy or these taxonomies. We add the labels array that defines the taxonomies as follows:

Now the args array should call the preceding labels array and also contain a Boolean for the hierarchy of the taxonomies.

When we combine the registration function, labels array and the args array of the custom taxonomies the full code should be:

Let us now see the results on our site after adding this code to function.php. If you have followed correctly you should now see the hotels categories under the hotels section. You can now click and add categories to organize your hotels custom posts.

How to Create Custom Post Types

Step 4: Creating Custom Post Type Template to Display Content

Custom post type when displayed they take the single.php file as the default display. Nevertheless we can custom page template to display the custom posts types. To create a custom page template we need to always remember that custom post type template should be saved as single-{post_type}.php  while the archive page should be saved as archive-{post_type}.php. We should now create a new file and add the following custom page template code and save it as single-hotel.php

If you look at the code above you will note I created an array that defined the post type that should be passed in the WordPress loop. The rest of the code is the get header, get footer and default WordPress loop; you can also add more code to enhance the way the final custom post is displayed.

Step 5: Publishing and Testing Custom Post

After creating the custom post types then adding custom taxonomies and finally creating custom post type page template, it’s time to see how it works. You can now go to add nee hotel and fill out the details as shown in the image below:

How to Create Custom Post Types

After adding this content and the image for the hotel, you can check to see how the custom post is displayed. As you can see I added the single-hotel.php to the evolve theme and WordPress has used it to display the custom post for hotels.

How to Create Custom Post Types

Conclusion

We have successfully learnt how to build custom post type programmatically and we can also enhance this custom post by adding metaboxes that enhance how the content for each of the hotels is displayed. For this tutorial the objective was to outline how to create custom post type programmatically in WordPress. To sum up what we have learnt; a custom post type is not very different from default post_types but can be our path to innovation in WordPress.

We can use the custom post type to build content management systems that clients conceptualize. I hope this tutorial has helped you understand the basic tenets of creating custom post types in WordPress. If you have additional questions, comments or complements do not hesitate to contact me via the comments section below.