WordPress Custom Post Types

How to Create Custom Post Types for Your WordPress Themes

Today I'll will explain how to create custom post types in WordPress themes and how they can be used to add new content types to your website.


WordPress is a powerful content management system that allows you to create and manage various types of content on your website. WordPress comes with several post types such as posts, pages, and attachments. Sometimes you may need to create custom post types to add new content types to your website.

What are Custom Post Types?

Custom post types are new content types that you can create in WordPress. They are similar to posts and pages, but they provide a way to organize and display different types of content on your website. For example, if you have a real estate website, you might want to create a custom post type for properties. This will allow you to add new properties in a structured way and display them on your website using a custom template. This is a brilliant way, when working with clients, to both limit and structure their options. That way, all they have to do is fill in the fields.

How to Create Custom Post Types in WordPress Themes

Creating custom post types in WordPress is a simple process for a developer. Even for beginners. You can create them using a plugin or by adding code to your WordPress theme’s functions.php file. I will show you how to create them by adding code to your theme’s functions.php file, as adding an extra plugin for such a simple procedure is overkill.

Step 1: Open your functions.php file

To get started, you need to open your theme’s functions.php file. You can do this by navigating to your theme (hopefully child theme) folder. Then, select your theme’s functions.php file in the directory and open it using your code editor. Personally, I use Visual Studio Code.

Step 2: Add code to create custom post type

Next, you need to add code to create your custom post type. Here’s an example of how to create a custom post type for properties:

function properties_post_type() {
    $args = array(
        'public'    => true,
        'label'     => __( 'Properties', 'textdomain' ),
        'supports' => array( 'title', 'editor', 'thumbnail' ),
        'menu_icon' => 'dashicons-admin-home',
    );
    register_post_type( 'properties', $args );
}
add_action( 'init', 'properties_post_type' );

In the code above, I’ve defined a custom post type called “Properties”. I’ve also specified some settings for this post type, such as the supports array that lists the features we want to enable (title, editor, and thumbnail), and the menu_icon parameter that specifies the WordPress Dashicons icon to use for this post type in the admin menu.

This example is a very simple version, as you actually have lot more options when setting up a Custom Post Type. Check out the WordPress Codex to see all the options.

Step 3: Save the changes

Once you’ve added the code to your functions.php file, you need to save the changes. And that’s about it! If everything went according to plan, then you’ll find the new menu item in the WordPress Dashboard. Key, when setting up a new Custom Post Type, is to re-save your Permalinks. Simply go to Settings and Permalinks. On the page you click Save and that’s it. This is important, else you might not be able to see your new posts.

Using Custom Post Types in WordPress Themes

Now that you’ve created your custom post type, you can start using it in your WordPress theme. To display your custom post type, you need to create a custom template file. You can do this by creating a new file in your theme’s folder and naming it single-{post_type}.php. For example, if your custom post type is called “Properties”, then you would create a file called single-properties.php.

If you don’t create a new template file, the default file is the single.php file. I usually copy my single.php file and rename it single-{post_type}.php file, to make sure it aligns with the theme.

In the custom template file, you can use WordPress functions to display the content of your custom post type. For example, you can use the_title() function to display the title of the post, and the_content() function to display the content.

What’s next?

After setting up my post type, the natural next step for me, is to create the custom fields for the Properties post type. This I do using Advanced Custom Fields, which makes it a lot easier for developers, to create unique fields on WordPress websites.