Quick Tip: Creating Custom Post Types without A Plugin in WordPress

If you’re creating a custom theme with even a little bit of complexity, chances are, you need some kind of custom post type. There are a few ways to approach it, some involving plugins. The CPT UI plugin is great for managing more complex post types with an intuitive interface. Though, like anything else in WordPress, you don’t always need to throw a plugin at it.

When Not To Use a Plugin

There are a couple of cases when managing one less plugin is a good idea. The first is if your needs are fairly simple. If you need 2-5 custom post types with simple options, you can get by without a plugin. If you don’t need to update and modify how your custom content works, defining it in code will be pretty simple. Maybe your needs are complex and you don’t mind modifying code for every change.

If you have non-developers who change post types often, I’d recommend going with the plugin! Ultimately, it’s up to your personal preference and what is easier for you. At the end of the day, there is no right or wrong way.

Creating a Simple Post Type

This article assumes you already know what a post type is and acts as a quick reference. Here’s how you can quickly get up and running.

function create_projects_post_type() {
    register_post_type( 'projects',
        [
        'labels' => [
            'name' => __( 'Projects' ),
            'singular_name' => __( 'Project' )
        ],
        'public' => true,
        'has_archive' => true,
        'show_in_rest' => true,
            'supports' => ['editor', 'title', 'excerpt']
        ]
        ]
    );
}

//All you need is an action and the function with options
add_action( 'init', 'create_projects_post_type' );

All you really need is the register_post_type function and an add_action call. The options array contains the basic options you usually need. labels are for labeling/organizing your post type, public and archive determine if your content is query-able. show_in_rest isn’t necessary unless you need extra post features such as excerpt, meta, and so on.

Once you include this file in your functions.php, your post type should pop up in your left sidebar! I’d recommend using a separate file and including it in your main functions file for better separation. What if you want to add more complex options? Easy!

Adding More Options

Adding more options is as simple as looking at the docs for register_post_type. As you can see, there are many available options. If you use something like CPT UI, keep in mind that every option there is also possible in register_post_type. Sometimes it comes down to preference. Once you get the hang of the function, you can create post types just as quickly as you would with any kind of plugin.

Help! My Post Types Aren’t Showing Up

On a somewhat related note, there are bugs that can happen when you add a new custom post type. Sometimes, after creating and adding a post, the permalink won’t show any content. This probably doesn’t mean you incorrectly configured the post type, it’s actually a permalink issue.

You can find the page in Settings->permalinks in any kind of WordPress install.

On that page, click “save changes”(you don’t have to actually change the permalink structure). Once you do this, it does some work behind the scenes to update all site permalinks. I’m not completely sure why, but it seems to fix common custom post type issues, so give it a shot if you’re stuck!

Conclusion

You can create a custom post type with a plugin, but sometimes doing it with code is quicker and easier. Plus, relying on one less plugin never hurts. You can make them as simple or as complex as you need and organize it into its own file. Hopefully you found something in this little guide helpful!

Comments