Skip to content

How to Create a WordPress Custom Post Type

custompostype

​​WordPress has multiple post types to help you organize your website’s content.

WordPress Custom post types are essentially different content structures that you can create to better suit your website’s needs. For example, if you’re running a website about movies, you might create a custom post type called “Movies” to store and display movie-related content.

Read more about WordPress Custom Post Type in our given article

Imagine your WordPress website as a big bookshelf, and each type of content is a different section on that shelf. Normally, WordPress gives you a few default sections like ‘posts’ and ‘pages.’ But what if you have special stuff that doesn’t fit in those sections? That’s where a “custom post type” comes in. It’s like creating your own unique section on the bookshelf to organize your special content in a neat and tidy way. This is super handy when your website has all sorts of different things, and you want each thing to have its own special spot.

Why do I Need to Create Custom Post Types?

Absolutely, here are some compelling reasons why utilizing WordPress custom post types can greatly benefit your website:

Diverse Content Organization: 

With WordPress custom post types, you can neatly categorize and manage different types of content that don’t fit into the standard ‘posts’ and ‘pages’ categories. This keeps your content well-organized and makes it easier for visitors to find what they’re looking for.

Tailored User Experience: 

Custom post types allow you to create a unique and tailored experience for your website visitors. You can present different types of content in their own specialized formats, enhancing user engagement and satisfaction.

Better SEO: 

Using custom post types can improve your website’s search engine optimization (SEO). By categorizing content more precisely, search engines can better understand your site’s structure, leading to potentially higher search rankings.

Enhanced Design Flexibility: 

Custom post types enable you to design and display content in specific ways that suit its purpose. Whether it’s a portfolio, events calendar, or testimonials, you have greater control over the layout and design.

Scalability and Growth: 

As your website evolves, custom post types ensure you’re prepared for growth. Adding new types of content won’t clutter your existing content structure, allowing your website to expand seamlessly.

WordPress comes with a few different post types by default:

Posts – category for regular posts. Typical content of this type includes a blog post.

Pages – displays static content that is not listed by date.

Attachments – all the website media, including images, videos, or PDF files.

Revisions – saved versions of all post types.

Navigation Menus – list of links your website visitors can use to find specific information.

Custom CSS – type that you can modify to customize your WordPress theme.

Changesets – the track of changes made in the customizer.

WordPress custom post types allow you to give yourself the freedom to select a specific area on your website for your content. With WordPress custom post types, you have the ability to swiftly gather content together without the need to pick categories, making it effortless to keep an eye on posts across various sections. This feature enables you to conveniently categorize your content without the hassle of traditional categories, ensuring that your posts remain well-organized within their designated sections.

Building Blocks of a WordPress Custom Post Type 

Before creating custom post types, you must understand the terms array and element. They are essential parts of your custom post type’s code.

What is an array and how are we supposed to use it in WordPress custom post type ?

An array is a fundamental data structure in programming that holds a collection of values or elements, each identified by an index or a key. It allows you to store and organize multiple pieces of related data in a single variable, making it easier to manage and manipulate large sets of information in a coherent manner.

Reason : 

Using custom post types in WordPress involves a few key steps. First, you define the new post type, specifying its name, labels, and desired attributes. Then, you can set up additional options like supporting specific features such as thumbnails, excerpts, and custom fields. Next, you’ll need to create templates to display the custom post type’s content on your website. Finally, you can start adding content to your custom post type, utilizing the distinct section you’ve created to organize and showcase your unique content in a way that best suits your website’s purpose.

To create a custom WordPress post type, there are three arrays containing different elements – $args, $labels, and $supports. The associative array $args is short for arguments, which include various key-value pairs, including:

description – a short and descriptive summary of the post type. You can only display it in the post type’s template.

public – changes WordPress custom post types’ visibility to authors and visitors. Choose TRUE to make it appear in the WordPress Dashboard and visitors’ custom query.

menu_position – defines the new post type’s location in the WordPress admin menu. Refer to the WordPress codex page for its value and positions.

has_archive – defines the custom post type archives, URL structure, and slug name based on parameter 1 of register_post_types().

show_in_admin_bar – alters the post type’s visibility in the upper admin bar under the +New section.

show_in_nav_menus – changes whether you can add posts of this type to navigation menus created via the AppearanceMenus.

query_var – sets whether site visitors can enter a post’s type and title as a query in the URL to view it.

Aside from the elements, the variable $args also encompasses the arrays $labels and $supports, both of which consist of various key-value pairs. It is advised to establish a distinct $labels variable, dedicated to containing these key and value pairs. This approach enhances code readability and comprehensibility.

The $label array defines the custom post type’s text and holds the following keys:

name – the general plural name for the post type
singular_name – the name for a single post of the custom type
add_new – replaces the ‘Add New’ text
add_new_item – changes the ‘Add New Post’ text
edit_item – adjusts the ‘Edit Post’ text
featured_image – modifies ‘Featured Image’ in the post editor
set_featured_image – replaces ‘Set Featured Image’
menu_name – alters the text of the top-level link. The default text for the link is the name key

Meanwhile, the $supports array contains elements that define the custom post type’s features. The TRUE value enables the following features:

title
editor
author
thumbnail
excerpt
trackbacks
custom-fields
comments
revisions
page-attributes
post-formats

Ways to Create a WordPress Custom Post Type

We’ll show you two methods, and also cover some ways you can diplay custom post types on your WordPress website.

By using a basic Plugin: This is the simplest method where you create a regular plugin. However, a drawback is that uninstalling or deactivating the plugin will also remove the custom post type.

Editing Theme’s functions.php: You can directly modify the functions.php file of your theme. While this doesn’t demand extra tools, any changes made this way might not survive a theme update.

Developing a Site-Specific Plugin: This approach involves creating a plugin exclusively for your site. It’s more intricate but offers the advantage of data preservation and high customization. Yet, it does require a certain level of technical proficiency.

Create WordPress custom post type by using a plugin

Easy way to create a WordPress Custom post type is by using a plugin. This method is recommended for beginners because it is safe and super easy.

STEP 1 : First you have to install and activate the Custom Post Type UI plugin

STEP 2 : After you activate the plugin, follow these steps:

Go to “CPT UI” in your WordPress admin menu.
Choose “Add / Edit Post Types” to make a new custom post type.
Once there, you’ll see different tabs. Select the one labeled “Add New Post Type.”

This is where you can create your own special type of content!

Create a WordPress Custom Post Type

First, you’ll need to come up with a short and simple name for your custom post type, like ‘movies’. This name will show up in the website’s address and in the WordPress system, and it should only use letters and numbers. Below that, you’ll want to provide both the name for when there’s more than one of these posts (plural) and the name for just one (singular).

If you want, you can then click on a link that says ‘Populate additional labels based on chosen labels’. This can save you time by automatically filling in the other label fields below.

STEP 3 : Now, scroll down to the section called ‘Additional Labels’. If you didn’t click the link we mentioned earlier, this is where you’ll describe your post type and set different labels.

image 6

These labels will be used throughout the WordPress user interface when you are managing content in that particular post type.

STEP 3 : Following that are the post type configurations. In this section, you can define various characteristics for your post type. Each choice is accompanied by a short explanation of its purpose.

image 8

For instance, you can choose not to make a post type hierarchical like pages or sort chronological posts in reverse.

Below the general settings, you will see the option to select which editing features this post type would support. Simply check the options that you want to be included.

image 7

Finally, click on the ‘Add Post Type’ button to save and create your custom post type.

That’s all. You have successfully created your custom post type and can go ahead and start adding content.

Create WordPress custom post type by Editing Theme’s functions.php file 

STEP 1 : Create a Child Theme

If you haven’t already, create a child theme of your main theme. This ensures your changes won’t be lost during theme updates. Create a new directory for your child theme and include a style.css file with the required header.

image 9

To create child theme watch our video easy way to create child theme using plugin or php code 

STEP 2 : Move functions.php to Child Theme

Locate the functions.php file in your main theme’s directory. Copy and move this file to your child theme’s directory.

image 10

STEP 3 : Edit functions.php in Child Theme and add Custom post type code 

Open the functions.php file in your child theme’s directory using a code editor. Here’s where you’ll be adding your custom post type code.

Copy this code and paste in your function.php in child theme 

// custom post type

function my_second_post_type()
{
	$doolb = array(

		'labels' => array(
			'name' => 'Donors',
			'singular-name' => 'Donor'
		),

		'heirarchical' => true,
		'public' => true,
		'has_archive' => true,
		'supports' => array('title','content','featured-image','custom-fields',),
	);

	register_post_type('donors', $doolb);
}

add_action('init', 'my_second_post_type');

After this your custom post type will be formed and will be shown on your dashboard 

Now if you want to create a taxonomy of your custom post type 

The code below 

function my_second_taxonomy()
{
	$group = array(
		'labels' => array(
			'name' => 'Blood-Groups',
			'singular-name' => 'Blood-Group'
		),
		'public' => true,
		'heirarchical' => true,
	);
	register_taxonomy('blood-groups', array('products', 'donors' ), $group);
}

add_action('init', 'my_second_taxonomy');

Create WordPress custom post type by creating custom post type plugin

It seems like you’re looking for guidance on creating custom post types in WordPress plugins using the register_post_type() function. Custom post types are a powerful feature that allows you to create and manage different types of content beyond just regular posts and pages. Here’s a step-by-step breakdown of the process:

  1. Create a Plugin:

 First, you need to create a plugin to contain your custom post type code. You can create a new folder within the wp-content/plugins/ directory of your WordPress installation and place your plugin files there. Make sure to give your plugin a unique name to avoid conflicts with other plugins.

  1. Define the Custom Post Type:

Inside your plugin’s main PHP file (often named after your plugin), you’ll define your custom post type using the register_post_type() function. This function takes two main parameters: the custom post type name (a unique identifier) and an array of arguments ($args) that define how the post type behaves and appears

function ht_register_custom_post_type() {
    $labels = array(
        'name' => 'Books',
        'singular_name' => 'Book',
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'books'),
    );

    register_post_type('book', $args);
}
add_action('init', 'ht_register_custom_post_type');

In this example, we’ve defined a custom post type named “book” with a public visibility, an archive page, and a custom URL slug.

  1. Hooking to the init Action:

The register_post_type() function should be called within a function that’s hooked to the init action. This ensures that the custom post type is registered when WordPress initializes. As shown in the example above, the add_action(‘init’, ‘ht_register_custom_post_type’) line adds your custom function to the init action.

  1. Prefix for Function Names:

As you mentioned, it’s a good practice to use a prefix for your custom function names to avoid conflicts with other plugins or WordPress core functions. In the example, I’ve used the “ht_” prefix.

  1. Customizing the Arguments:

The $args array in the register_post_type() function allows you to customize various aspects of your custom post type, such as labels, visibility, permalink structure, and more. You can find a list of available arguments and their explanations in the WordPress documentation: register_post_type() Arguments.

Remember that this example is quite basic. Depending on your needs, you can further extend the functionality and customization of your custom post type.

After adding this code to your plugin file and activating the plugin in your WordPress admin panel, you should see the “Books” menu item in the admin sidebar, and you’ll be able to create and manage books as a separate type of content.

if u want ti add Custom CSS in WordPress read our article How to add custom CSS in your WordPress website

or to make custom short code How to Create Custom Shortcode in WordPress | A Step by Step guide.

Scan the code