Custom Post Types in WordPress

Usually people falsely label WordPress as just a simple blogging CMS. Since its foundation, WordPress has developed into a vast robust Content Management System that grant end-users to build any design in mind. Modern attributes like: custom menus, custom widgets, custom post types, and custom post formats all give permission to users to design and build their websites in the best way they require.

Here in this article we will learn custom post types in WordPress. We will learn and study what custom post types are, why we use them and how they are build, we will also create an example custom post type so you will be able to create your own. Before we begin lets discuss some common questions about custom post types:

What is a Custom Post Type?

Custom posts are simple basic posts with different parameters inside the code. The regular post type is labelled as post, pages as page, and attachments as attachment and so on. Since the foundation of WordPress 3.0, you are now able to create your desired custom post types. If you create it successfully with the correct parameters returned you will have a great customised area on your site like no other.

Why Use a Custom Post Type?

Let’s say you want to partition your content in the easy structured way on your site. Sure, in your blog you can use pre-defined categories that is tags, posts, authors and you can use custom categories.

Let us consider news review section for a website. If you want to post a review for a news based on title popularity or featured image etc. All these fields and more can be simply added with custom post types.

1) Creating a Custom Post Type – Through Plugin

The easiest method to build a custom post type in WordPress is by using plugin. It is suggested for beginners because it is straightforward and super easy.

First you need to simply install and activate the Custom Post Type UI plugin. After activation, the plugin will show a new menu feature in your WordPress admin menu as CPT UI.

Now simply go to CPT UI » Add/ Edit Post Type and create new custom post type. The following is a default screenshot of this plugin main page:

1

Through Custom Post Type plugin UI you can also create custom taxonomies.

For creating your custom taxonomy if you want one simply go to CPT UI » Add/ Edit Taxonomies and create new custom taxonomy.

In Post Type Slug, you need to add URL for your desired custom post type. Your custom post type URL has to be within 20 characters e.g. news, sports, technology etc.

 

2

In the next area, you need to add a ‘Plural Label’ for custom post type. This will show up in your WordPress admin menu item area like posts and pages. To look attractive and sensible add your name in plural form, e.g. News, Mobile Phones, Deals etc.

After this step you need to add a singular form to your label.

Now just click ‘Create Custom Post Type’ button and your new custom post type is ready and your new custom post type is added now.

If you want to add more features or customise your custom post type just go further down on the same page and use the features of “Additional Labels” and “Settings” sections.

2) Adding Your Own Custom Post Type without plugin

The major issue that users face while using a plugin is that custom post type made through plugin will be removed when plugin gets disabled. However data will not be lost from those custom post types, but your custom post type will get unregistered and cannot be accessed through admin panel.

If you are functioning on the client site and don’t want to use a plugin, then you can easily create your own custom post type by just adding the necessary code in your theme’s functions.php file. In this example we will have a look to the code of adding custom post type using code.

3

Through this code we will register a post type ‘news’ with an array of arguments. These arguments will provide us the options for custom post type. We need to define this in an array, this array has different arguments such as public visibility, archive, slug etc. that we will be using in URLs for post type as parameters.

Now let us look the detailed part of code which will add more features to custom post type.

4

Now here you can see we have inserted many more features to the custom post type with this code. It will now add more attributes as support for revisions, featured image, custom fields, etc.

Here we have also linked our custom post type with a simple custom taxonomy as enews. We set the hierarchical value of custom post type to be false. If you want your custom post type to act like Pages, then simply set this value to true.

Also consider the duplicated use of twentysixteen theme, this is known as text domain. Your text domain can be easily found in style.css file in your theme directory. Text domain will be provided in the header part of this file.

 

Showing Your Post Type on the Website

For displaying your custom post type WordPress provides the built-in theme support. Once you added items in your newly created custom post type, it is time to show them up on your newly created website.

There are several methods you can use, each method has different benefits.

 

Link or URL of custom Post Type

The link of custom post types depends on the option that you have selected in Permalinks section of WordPress admin panel. If this option is set to “Post name” then the URL of our news custom post type will be:

http://example.com/news

You can replace example.com with the domain name you want and news with custom post type name you want.

 

Displaying Custom Posts on the Front End

The best advantage of using the custom posts is that it keeps partition between your regular posts and custom posts. However, if you want them to show up among your regular posts, then you can also do that by simply putting this code into your theme’s functions.php file or a WordPress specific plugin:

5

Do replace ‘news’ with your required custom post type name.

Displaying Custom Post Types in Widgets

There is a built-in widget in WordPress to show recent posts. But through this widget you cannot choose your custom post type. If you want to show your newly created entries in the custom post type then what will you do? There is a simple and user friendly way to do this.

Here what you have to do, you just need to simply install and activate the Ultimate Posts Widget plugin. After activation, go to Appearance » Widgets hold and drop the Ultimate Posts widget to your sidebar.

This useful widget will show your recent posts from each post type. Post can also be displayed without a ‘read more’ link or you can even display a featured image next to your post label. Now from here you can configure the widget by using the options you want and by selecting custom post type. Now simply save the changes and access your widget from the action on your site.

A Template Structure for Your Post Type

WordPress has a template hierarchy to categorize the template file of your theme for displaying the given content type. It’s above the capacity of this post to learn about the template hierarchy and how it works in detail, but it helps a lot to understand how WordPress shows newly build custom post types.

When WordPress shows a product, it will search in order for these file names in your theme:

  1. Single product displaying file, known as single-product.php.
  2. File showing all single post of any post type, known as single.php.
  3. Generic file for showing every kind of content, known as index.php.

When WordPress shows a product archive, it will look for these template files:

  1. Product archive displaying file, known as archive-product.php.
  2. Generic archive displaying template file, known as archive.php.
  3. Default template, index.php.

WordPress functions through this hierarchy and utilize the template file which comes first. So while creating a custom template for your website, the best procedure to start with is by creating a single.php file, saving this file as single-product.php and editing it as you require.

In any case if you do not desire to build own template files, WordPress will happily access the files present in your theme, which is what it’s done for me with the twentyfifteen theme’s archive.php and single.php files.

Final Thoughts

In this article you have studied how to build custom post types and taxonomies in WordPress. You have also studied how to display posts using templates and custom queries. I hope this tutorial was helpful to you.