Custom post types are a powerful feature in WordPress that allow users to create and manage custom content types in their WordPress site. This can be especially useful for businesses or organisations that need to manage complex or specialised content, such as products, events, or job listings.

In this article, we will outline the steps for coding a custom post type in WordPress. While this process involves some technical knowledge, it is not necessarily difficult and can provide a great deal of flexibility and functionality to your WordPress site.

  1. Create a new plugin: The first step in creating a custom post type is to create a new plugin. This will allow you to store your custom post type code in a separate file that can be activated and deactivated as needed. To create a new plugin, create a new folder in the “wp-content/plugins” directory of your WordPress site and add a new PHP file inside the folder. The name of the folder and the PHP file will be the name of your plugin.
  2. Add the plugin header: The plugin header is a block of code that provides information about your plugin, including the plugin name, version, and author. Add the following code to the top of your plugin file:
<span class="hljs-meta"><?php</span>
<span class="hljs-comment">/*
* Plugin Name: Custom Post Type Plugin
* Plugin URI:
* Description: A custom post type plugin
* Version: 1.0
* Author: Your Name
* Author URI:
  1. Register the custom post type: To register a custom post type, you will need to use the “register_post_type” function. This function takes an array of arguments that specify the details of the custom post type. Here is an example of how to register a custom post type called “products”:
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">create_product_post_type</span>() </span>{
<span class="hljs-title function_ invoke__">register_post_type</span>( <span class="hljs-string">'product'</span>,
<span class="hljs-keyword">array</span>(
<span class="hljs-string">'labels'</span> => <span class="hljs-keyword">array</span>(
<span class="hljs-string">'name'</span> => <span class="hljs-title function_ invoke__">__</span>( <span class="hljs-string">'Products'</span> ),
<span class="hljs-string">'singular_name'</span> => <span class="hljs-title function_ invoke__">__</span>( <span class="hljs-string">'Product'</span> )
<span class="hljs-string">'public'</span> => <span class="hljs-literal">true</span>,
<span class="hljs-string">'has_archive'</span> => <span class="hljs-literal">true</span>,
<span class="hljs-string">'rewrite'</span> => <span class="hljs-keyword">array</span>(<span class="hljs-string">'slug'</span> => <span class="hljs-string">'products'</span>),
<span class="hljs-string">'supports'</span> => <span class="hljs-keyword">array</span>(<span class="hljs-string">'title'</span>, <span class="hljs-string">'editor'</span>, <span class="hljs-string">'author'</span>, <span class="hljs-string">'thumbnail'</span>, <span class="hljs-string">'excerpt'</span>, <span class="hljs-string">'comments'</span>)
<span class="hljs-title function_ invoke__">add_action</span>( <span class="hljs-string">'init'</span>, <span class="hljs-string">'create_product_post_type'</span> );

The “labels” array specifies the labels for the custom post type, such as the name and singular name. The “public” option determines whether the custom post type is visible to the public. The “has_archive” option determines whether the custom post type has an archive page and specifies the URL slug for the custom post type’s archive page. The “supports” array specifies the features that are supported by the custom post type, such as the ability to add a title, editor, or thumbnail.

  1. Customise the custom post type: After registering your custom post type, you may want to customise it further. This can be done by adding custom fields or taxonomies, or by modifying the way the custom post type is displayed on the site.

To add custom fields, you can use a plugin like Advanced Custom Fields or create your own custom fields using the “add_meta_box” function. The “add_meta_box” function allows you to create a custom meta box that appears on the edit screen for your custom post type. You can then use the “update_post_meta” function to save the data entered in the custom field.

To add taxonomies, you can use the “register_taxonomy” function. This function allows you to create custom categories or tags for your custom post type. For example, you might create a “product_type” taxonomy for a products custom post type.

To modify the way your custom post type is displayed, you can create custom templates for your custom post type. To do this, create a new template file in your theme folder and specify the custom post type in the template file’s header. For example, you might create a “single-product.php” template file to control the display of individual product posts.

You can also use shortcodes to display your custom post type in specific locations on your site, such as in a widget or on a page. To do this, you can use the “add_shortcode” function to create a custom shortcode for your custom post type.

  1. Test and activate your custom post type: Once you have completed the code for your custom post type, test it on your WordPress site to make sure it is working correctly. If everything is working as expected, activate your plugin to make the custom post type live on your site.

In conclusion, coding a custom post type in WordPress requires some technical knowledge, but it is a powerful feature that can greatly enhance the functionality and flexibility of your WordPress site. By following the steps outlined above, you can easily create and customise a custom post type to meet the specific needs of your business or organisation.

Let's Get In Touch!

Ready to start your project? Get in touch to have a chat!