How to Create Custom Post Types in WordPress

create custom post types wordpress

Custom post types in WordPress enable you to build dedicated sections for different kinds of content other than the default posts/pages. Instead of mixing everything together, you can build unique layouts and structures for items like products, portfolios, or movie reviews. It keeps your website structured and allows visitors to quickly locate the details they are looking for. Developers often use the WordPress Post API to create these post types through custom plugins. This gives each type its own fields, categories, and tags for better management.

WordPress includes several default post types by design, like posts, pages, media attachments, content revisions, and navigation menus. Custom post types extend this functionality, allowing you to tailor your site to its purpose. For instance, an eCommerce site may have a “Products” post type, a portfolio site could feature “Projects”, and so on. Even popular plugins like WooCommerce, WPForms, and MemberPress rely on custom post types to handle their content. This shows how essential they are for building a flexible and user-friendly website.

In this guide, we’ll explain the process of building custom post types in WordPress.

Create Custom Post Types WordPress via WPCode

Typically, setting up a custom post type in WordPress requires inserting code into the functions.php file of your theme. The problem is that even a small error can cause your site to break, and if you update or change your theme, the code you added will be lost. That’s why editing functions.php directly is not recommended unless you’re an experienced developer.

The WPCode plugin offers a simpler and more secure option. This tool lets you insert custom code snippets without touching your theme files and comes with a built-in library of ready-to-use snippets. It can also replace several single-purpose plugins, making your site lighter and easier to manage.

To create custom posts using WPCode, first, you need to install and activate the WPCode plugin:

install wpcode plugin

After activating the plugin, go to your WordPress dashboard and click on Code Snippets > Add Snippet:

add snippets with wpcode

Here, multiple code snippet templates are available; choose according to your preferences:

snippet templates

For instance, we select the “Add your Custom Code” option and then enter a title for our code snippet:

create custom snippet

Next, paste the code below into the “Code Preview” box to create a custom post type called “Books”. This custom post will show up in your WordPress admin menu and can work with any theme:

// Function to register a custom post type
function register_books_post_type() {
    $labels = array(
        'name'               => __( 'Books' ),
        'singular_name'      => __( 'Book' ),
        'add_new'            => __( 'Add New Book' ),
        'add_new_item'       => __( 'Add New Book' ),
        'edit_item'          => __( 'Edit Book' ),
        'new_item'           => __( 'New Book' ),
        'all_items'          => __( 'All Books' ),
        'view_item'          => __( 'View Book' ),
        'search_items'       => __( 'Search Books' ),
        'not_found'          => __( 'No books found' ),
        'not_found_in_trash' => __( 'No books found in Trash' )
    );
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'has_archive'        => true,
        'rewrite'            => array( 'slug' => 'books' ),
        'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'show_in_rest'       => true,
    );
    register_post_type( 'books', $args );
}
add_action( 'init', 'register_books_post_type' );

If you want a simple setup, just replace the word “books” in the code with your own slug and change the labels to match your content type. Once done, click Update to save it.

Build a Custom Post with the CPT UI Plugin

An alternative, easy-to-use method for handling custom post types in WordPress is through the “Custom Post Type UI” plugin. First, install and enable the CPT UI plugin, then go to your WordPress dashboard, and navigate to CPT UI > Add/Edit Post Types to create your custom type:

add or edit custom post types with cpt ui

You’ll be asked to define a slug, like books or movies, which WordPress will use in URLs and queries. The slug must consist only of letters and numbers. You’ll also provide the plural and singular names, and if you want to save time, you can use the ‘Populate additional labels’ option to auto-fill the extra fields. Otherwise, you can manually add a description and adjust labels in the Additional Labels section. The labels you set here will appear across the WordPress dashboard when managing content for that post type.

After this, you can move on to the settings, where you’ll find different options to configure its attributes, each with a short description to guide you:

configure additional attributes cpt ui

For example, you can decide whether your post type should behave like pages with a hierarchy or like posts arranged by date. Under the general settings, you’ll also find options to choose which editing features to enable. Just check the ones you need, and when you’re done, click “Add Post Type” to save and create it:

choose editing features to enable

That’s it! Your new custom post type is set up, and you can start creating content for it immediately.

Final Thoughts

Custom post types provide an effective method to expand WordPress beyond regular posts and pages, making your site more structured and customized to your requirements. Whether you prefer the coding approach with WPCode or the beginner-friendly CPT UI plugin, both methods let you create and manage custom post types easily. Once set up, you can easily organize different types of content, improve user experience, and build a site that truly fits its purpose. In this WordPress custom post type tutorial, we demonstrated various ways to set up custom post types.

Creating custom post types in WordPress can be tricky, especially when organizing content for different sections of your site. Ultahost’s managed VPS hosting offers dedicated resources and easy-to-use management tools, making it simpler to build and manage custom post types while keeping your website fast and efficient.

FAQ

What does a custom post type mean in WordPress?
Why should I use custom post types?
Is it possible to build custom post types without coding?
What’s the difference between WPCode and CPT UI for custom post types?
Will my custom post type work with any WordPress theme?
Can I add extra fields to my custom post types?
Are custom post types SEO-friendly?

Related Post

wordpress amazon s3

Integration Guide for WordPress Amazon S3 and...

Large files on your website negatively impact loading t...

wordpress sendgrid

How to Configure SendGrid in WordPress to Sen...

WordPress can send basic emails like registration and p...

How to Install and Setup WordPress Multisite ...

WordPress Multisite is a powerful feature that allows y...

How to Move WordPress from Local Server to Li...

Search engines like Google cannot index websites hosted...

How to Display Related Posts in WordPress

Engaging your readers and keeping them on your WordPres...

How to Fix WordPress Not Sending Email Issue

WordPress is a powerful content management system used ...

Leave a Comment