Asset 20 8 2
Does AI recommend your business? Run the free check →

Join 15,000 business owners, marketers and entrepreneurs. The Sunday newsletter you'll be annoyed only arrives once a week.

Article

How to Add a Custom Post Type in WordPress (Code or Plugin)

Register a custom post type with register_post_type() on the init hook, ideally inside a plugin rather than your theme. Give it a short prefixed key of 20 characters or fewer, set public to true, and add show_in_rest if you want the block editor. Or use a plugin such as Custom Post Type UI or ACF.

When posts and pages aren't enough

WordPress ships with five default post types: post, page, attachment, revision and menu. That covers a blog and a few service pages. But if you're adding case studies, team members, recipes or products, mixing them into Posts makes your archive messy and your admin harder to use. A custom post type gives that content its own menu, its own archive and its own URL.

There are two ways in: a few lines of code, or a plugin. Both end in the same place.

The code route, step by step

1. Pick the post type key

Choose a short, prefixed key of 20 characters or fewer. It can only contain lowercase letters, numbers, dashes and underscores. The 20-character limit exists because the post_type database column is a VARCHAR of that length. Don't start it with wp_, which core uses, and avoid generic keys like product that another plugin may also register. Something like acme_case_study works.

2. Put the code in a plugin

The handbook recommends a plugin rather than your theme, so the content stays registered if you change themes. Put it in the theme and switch designs, and your case studies vanish from the admin.

3. Register the post type

Write a function that calls register_post_type() with your labels, public set to true and has_archive set to true. Add show_in_rest set to true, or the type won't be available in the block editor. The supports argument lists the features you want, such as title, editor, comments, revisions, author, excerpt and page-attributes. For case studies, I'd pick title, editor, excerpt and revisions, and leave comments out unless you want readers replying on each one. Keep the list short; you can add features later by editing the same array.

function acme_register_case_study() { register_post_type( 'acme_case_study', array( 'labels' => array( 'name' => 'Case Studies', 'singular_name' => 'Case Study' ), 'public' => true, 'has_archive' => true, 'show_in_rest' => true, 'rewrite' => array( 'slug' => 'case-studies' ) ) ); } add_action( 'init', 'acme_register_case_study' );

4. Set a cleaner URL

By default, the URL looks like example.com/wporg_product/post-name, using the key. Change it with the rewrite array's slug key, as in the example. Pick something specific, because slugs can conflict with other plugins.

5. Hook it to init

Register the type after after_setup_theme and before admin_init. The init action is the right place, which is what add_action does in the snippet.

6. Flush the rewrite rules

If you register the type in a plugin, call flush_rewrite_rules() on activation and deactivation, or simply visit Settings > Permalinks and save.

New URLs returning 404s

WordPress Developer Resources flags the two problems people hit most. The key is capped at 20 characters because of that database column, so a long descriptive key simply won't work. And a registration that's correct can still return 404s on its URLs until the rewrite rules are flushed. If your new archive page shows 'not found', go to Settings > Permalinks and save before you change any code.

Work with me

Want AI doing the heavy lifting in your marketing?

I build the systems that handle the boring 80 percent, so you get your week back. Done properly, with the human kept in.

The no-code route with a plugin

Custom Post Type UI gives you an interface for registering and managing custom post types and taxonomies with no coding.

Advanced Custom Fields has an ACF > Post Types screen with an Add New button. The required fields are Plural Label, Singular Label and Post Type Key, and the same key rules apply. It's a natural fit if you're already using it to add custom fields. For more ideas on what to install, see my list of WordPress plugins worth having.

My advice on which route to pick

If you're comfortable editing a small plugin file, use code: it's one function, it's portable and there's nothing extra to update. If you'd rather click, use Custom Post Type UI or ACF, but pick the key with the same care, because the same 20-character rules apply. Either way, flush permalinks straight after. If you're new to building sites, my beginner's guide to WordPress development fills in the basics.

Want your tool on this list? Featured spots and reviews here are paid partnerships. Here's how to partner with me.

Frequently asked questions

Where should custom post type code go?

In a plugin, not the theme, so the content stays if you switch themes.

How long can the key be?

20 characters at most, using lowercase letters, numbers, dashes and underscores.

Why doesn't my post type show in the block editor?

Set show_in_rest to true.

Why do my new URLs 404?

Flush the rewrite rules, for example by saving the Permalinks screen under Settings.

Can I do this without code?

Yes, with plugins such as Custom Post Type UI or ACF.

Published and maintained by the Lilach Bullock team, covering marketing, AI and business growth.
Your buyers are asking AI who to use. Does it say you?

See for free whether ChatGPT, Claude, Perplexity, Gemini and Google name you, and get the plan to become the answer.

Check my AI visibility →
Sundays only

Get the Sunday newsletter.

One email a week. AI experiments, marketing tactics, and the workflows Lilach is building right now in her own business.

Subscribe free

Let’s get your marketing running on AI.

Book a free 30-minute call

We figure out what you need, where AI fits in, and what working together would look like.

Book the call →

Or take the 30-second calculator

You’ll see the hours and the money quietly leaking out of your week, and the three workflows worth building first.

Take the calculator →

Or grab the free AI resource library

Prompt packs, templates, checklists, and swipe files. The exact tools I build for paying clients. Yours, free.

Get the library →
Keep reading

More from the blog.