Menu

Resources

How to Create Custom Post Types in WordPress With register_post_type()

Register a WordPress custom post type in code, add a taxonomy and a checkbox field, show it in the block editor and REST API, and fix the usual 404s.

By: Irina ShvayaNovember 17, 2026

Reviewed by Ben GuntherLast reviewed

A woman in a purple sweater looking at her laptop

This guide is part of our WordPress resource hub: costs and hosting, the block editor, plugins, SEO and speed, security, maintenance, WooCommerce, development, comparisons and migrations.

Key Takeaways

  • You create a custom post type by calling register_post_type() on the init hook, ideally inside a small plugin so the content survives a theme change.
  • The post type key must be 20 characters or fewer, lowercase, and prefixed; never use core names such as post, page or wp_template.
  • Set show_in_rest to true or the post type will open in the classic editor and stay out of the REST API.
  • Attach a taxonomy with register_taxonomy() and store extra fields such as checkboxes with register_post_meta().
  • If new posts return 404 errors, flush rewrite rules once by saving Settings > Permalinks or on plugin activation.

To create a custom post type in WordPress without a plugin, call register_post_type() inside a function hooked to init, give it a short prefixed key, and pass an arguments array that sets labels, public, has_archive, rewrite, supports and show_in_rest. Put that code in a small custom plugin rather than your theme, then visit Settings > Permalinks once so the new URLs work.

The rest of this guide gives you a complete, working plugin file, explains the arguments that matter, and shows how to add a taxonomy, a checkbox field, templates and REST access.

When a Custom Post Type Is the Right Tool

WordPress ships with posts (dated, shown in feeds and archives) and pages (static, can have parents). A custom post type (CPT) is a third kind of content with its own admin menu, its own URL base, its own fields and its own templates.

Use a CPT when a group of content has a different shape from blog posts. Typical examples:

  • Projects or case studies with a location, completion date and gallery.
  • Team members or providers with credentials, specialties and an office.
  • Listings, events or courses that need their own archive and filters.

If the content is just a different topic of blog post, a category is enough. If you need one standalone page, use a page. A common mistake is a "Case Studies" blog category that later needs custom fields and a different layout; moving that to a CPT afterwards means changing URLs and redirects.

Step 1: Create a Small Plugin for the Code

The WordPress Plugin Handbook recommends registering post types in a plugin rather than a theme, because post types registered in a theme disappear when someone switches themes. The content stays in the database, but WordPress no longer knows how to show or edit it.

Create a folder such as wp-content/plugins/acme-projects/ and a file acme-projects.php inside it. A must-use plugin (a single file in wp-content/mu-plugins/) also works and cannot be deactivated from the admin. Replace the acme prefix with your own short prefix everywhere.

<?php
/**
 * Plugin Name: Acme Projects
 * Description: Registers the Project post type and the Service Type taxonomy.
 * Version:     1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

Step 2: Register the Post Type With register_post_type()

Add this function to the same file. It registers a post type with the key acme_project and public URLs under /projects/.

function acme_register_project_post_type() {
	$labels = array(
		'name'               => _x( 'Projects', 'post type general name', 'acme' ),
		'singular_name'      => _x( 'Project', 'post type singular name', 'acme' ),
		'menu_name'          => _x( 'Projects', 'admin menu', 'acme' ),
		'add_new_item'       => __( 'Add New Project', 'acme' ),
		'edit_item'          => __( 'Edit Project', 'acme' ),
		'new_item'           => __( 'New Project', 'acme' ),
		'view_item'          => __( 'View Project', 'acme' ),
		'all_items'          => __( 'All Projects', 'acme' ),
		'search_items'       => __( 'Search Projects', 'acme' ),
		'not_found'          => __( 'No projects found.', 'acme' ),
		'not_found_in_trash' => __( 'No projects found in Trash.', 'acme' ),
	);

	register_post_type(
		'acme_project',
		array(
			'labels'        => $labels,
			'public'        => true,
			'has_archive'   => true,
			'rewrite'       => array(
				'slug'       => 'projects',
				'with_front' => false,
			),
			'menu_position' => 20,
			'menu_icon'     => 'dashicons-portfolio',
			'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'revisions', 'custom-fields' ),
			'show_in_rest'  => true,
			'rest_base'     => 'projects',
		)
	);
}
add_action( 'init', 'acme_register_project_post_type' );

Activate the plugin and a Projects menu appears in the admin. Because has_archive is true and the rewrite slug is projects, the archive lives at /projects/ and single items at /projects/item-name/.

Rules for the post type key

  • 20 characters maximum, lowercase letters, numbers, dashes and underscores only.
  • Prefix it (acme_project, not project) so it cannot clash with another plugin.
  • Do not use names core already registers: post, page, attachment, revision, nav_menu_item, custom_css, customize_changeset, oembed_cache, user_request, wp_block, wp_global_styles, wp_navigation, wp_template or wp_template_part. Avoid action, author, order and theme too, and do not start your own key with wp_.

If you searched for register_post_type( 'wp_template': that call lives in WordPress core. Block themes store templates you edit in the Site Editor as wp_template and wp_template_part posts. You read them, you do not register them.

The register_post_type() Arguments That Matter

The full list is on the register_post_type() reference in WordPress Developer Resources. These are the ones most sites need, with their defaults:

ArgumentDefaultWhat it does
publicfalseMaster switch. Sets the defaults for publicly_queryable, show_ui, show_in_nav_menus and exclude_from_search.
show_in_restfalseExposes the type to the REST API. Required for the block editor; without it you get the classic editor.
rest_basepost type keyThe REST route, e.g. /wp-json/wp/v2/projects.
has_archivefalsetrue creates an archive at the rewrite slug; a string sets a different archive slug.
rewritetrue (slug = key)Array with slug, with_front, feeds, pages. false disables pretty URLs.
hierarchicalfalsetrue allows parent and child items, like pages.
supportstitle, editorEditor features: title, editor, author, thumbnail, excerpt, trackbacks, custom-fields, comments, revisions, page-attributes, post-formats.
taxonomiesnoneTaxonomies to attach. They still need register_taxonomy().
capability_typepostBase for permission names. Use with map_meta_cap for custom roles.
menu_iconposts iconA Dashicons class, an image URL or a base64 SVG data URI.
template / template_locknone / falseDefault blocks for new items and whether editors can change them.

Two settings cause most confusion. First, thumbnail in supports only shows the Featured Image panel if the theme supports post thumbnails. Second, a CPT with public set to false is hidden from the front end and, unless you set show_ui, from the admin as well.

Step 3: Add a Custom Taxonomy to the Post Type

Taxonomies group your items, the way categories and tags group posts. Register one with register_taxonomy(), also on init, and pass your post type key as the second argument.

function acme_register_service_type_taxonomy() {
	register_taxonomy(
		'acme_service_type',
		array( 'acme_project' ),
		array(
			'labels'            => array(
				'name'          => _x( 'Service Types', 'taxonomy general name', 'acme' ),
				'singular_name' => _x( 'Service Type', 'taxonomy singular name', 'acme' ),
			),
			'hierarchical'      => true,
			'show_admin_column' => true,
			'show_in_rest'      => true,
			'rewrite'           => array( 'slug' => 'service-type' ),
		)
	);
	register_taxonomy_for_object_type( 'acme_service_type', 'acme_project' );
}
add_action( 'init', 'acme_register_service_type_taxonomy' );
  • hierarchical => true gives a category-style checklist; false gives a tag-style input.
  • show_in_rest => true is needed for the taxonomy panel to appear in the block editor.
  • show_admin_column => true adds a Service Types column to the Projects list screen.
  • Taxonomy keys can be up to 32 characters. The developer reference recommends calling register_taxonomy_for_object_type() right after registering, so queries during parse_request and pre_get_posts see the connection.

Step 4: Add Custom Fields, Including a Checkbox

Most CPTs need extra data. Register each field with register_post_meta() so WordPress knows its type and the block editor and REST API can read it. For meta on a custom post type to appear in the REST API, the post type must support custom-fields, which is why it is in the supports array above.

function acme_register_project_meta() {
	register_post_meta(
		'acme_project',
		'acme_featured',
		array(
			'type'              => 'boolean',
			'single'            => true,
			'default'           => false,
			'show_in_rest'      => true,
			'sanitize_callback' => 'rest_sanitize_boolean',
			'auth_callback'     => function ( $allowed, $meta_key, $post_id ) {
				return current_user_can( 'edit_post', $post_id );
			},
		)
	);
}
add_action( 'init', 'acme_register_project_meta' );

To give editors a checkbox, add a meta box. It works in both the classic and block editors; in the block editor it appears in the sidebar or below the content.

function acme_add_featured_meta_box() {
	add_meta_box(
		'acme_featured_box',
		__( 'Featured project', 'acme' ),
		'acme_render_featured_meta_box',
		'acme_project',
		'side'
	);
}
add_action( 'add_meta_boxes', 'acme_add_featured_meta_box' );

function acme_render_featured_meta_box( $post ) {
	wp_nonce_field( 'acme_save_featured', 'acme_featured_nonce' );
	$checked = (bool) get_post_meta( $post->ID, 'acme_featured', true );
	?>
	<label>
		<input type="checkbox" name="acme_featured" value="1" <?php checked( $checked ); ?> />
		<?php esc_html_e( 'Show on the homepage', 'acme' ); ?>
	</label>
	<?php
}

function acme_save_featured_meta( $post_id ) {
	if ( ! isset( $_POST['acme_featured_nonce'] )
		|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['acme_featured_nonce'] ) ), 'acme_save_featured' ) ) {
		return;
	}
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		return;
	}
	update_post_meta( $post_id, 'acme_featured', isset( $_POST['acme_featured'] ) );
}
add_action( 'save_post_acme_project', 'acme_save_featured_meta' );

The nonce, autosave and capability checks are not optional: without them anyone who can trigger a save could change the field. For many fields, a field plugin saves time. Advanced Custom Fields and its WordPress.org fork Secure Custom Fields both build field groups and can register post types and taxonomies from the admin; see our comparison of WordPress custom field plugins. If you want fields to feed core blocks directly, look at the Block Bindings API.

Step 5: Flush Rewrite Rules So the URLs Work

New post types add new URL rules. Until WordPress rebuilds its rewrite rules, single items and the archive return 404 errors. While developing, go to Settings > Permalinks and click Save Changes. In a distributed plugin, flush once on activation and deactivation:

function acme_projects_activate() {
	acme_register_project_post_type();
	acme_register_service_type_taxonomy();
	flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'acme_projects_activate' );

function acme_projects_deactivate() {
	unregister_post_type( 'acme_project' );
	flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'acme_projects_deactivate' );

Never call flush_rewrite_rules() on every page load. It is slow, and core documentation warns against it.

Step 6: Display the Post Type on the Front End

Classic (PHP) themes

WordPress looks for template files named after the post type key, not the rewrite slug:

  • single-acme_project.php, then single.php, then singular.php, then index.php.
  • archive-acme_project.php, then archive.php, then index.php.
  • taxonomy-acme_service_type.php for the taxonomy archive.

Put custom templates in a child theme if you use a third-party theme. To list featured projects anywhere, use a secondary query:

$featured = new WP_Query(
	array(
		'post_type'      => 'acme_project',
		'posts_per_page' => 6,
		'meta_key'       => 'acme_featured',
		'meta_value'     => '1',
	)
);

if ( $featured->have_posts() ) {
	while ( $featured->have_posts() ) {
		$featured->the_post();
		printf(
			'<h3><a href="%s">%s</a></h3>',
			esc_url( get_permalink() ),
			esc_html( get_the_title() )
		);
	}
	wp_reset_postdata();
}

Block themes

Block themes use HTML files in the theme's templates folder with the same names: single-acme_project.html and archive-acme_project.html. You can also create them without code in the Site Editor: open Templates, add a new template and pick the single item or archive for your post type. The Query Loop block can list your CPT as long as show_in_rest is true.

Step 7: Use the Post Type in the REST API

With show_in_rest set and rest_base set to projects, published items are available at /wp-json/wp/v2/projects, and terms at /wp-json/wp/v2/acme_service_type. The registered acme_featured field appears in each item's meta object. That is enough to feed a headless front end or another app; our WordPress REST API guide covers authentication and custom endpoints.

Plugin Option: When a UI Makes More Sense

Custom Post Type UI, ACF and Secure Custom Fields let you register post types and taxonomies from the admin with no code. That is fine for prototypes and for sites without a developer. The trade-offs:

  • The definitions live in the database and depend on the plugin. Deactivate it and the menus, URLs and templates for that content stop working until the type is registered again.
  • Changes are harder to review and move between staging and production than code in version control.
  • Most of these plugins can export PHP, so you can start in the UI and move the generated code into a plugin later.

Troubleshooting Custom Post Types

SymptomLikely causeFix
Items or archive return 404Rewrite rules not flushed, or the slug clashes with a pageSave Settings > Permalinks; rename the clashing page or slug
Classic editor instead of block editorshow_in_rest is falseSet show_in_rest to true
Taxonomy panel missing in the editorTaxonomy has show_in_rest falseSet show_in_rest on the taxonomy
Custom template ignoredFile named after the slug, not the keyRename to single-{post-type-key}.php or .html
register_post_type returns WP_ErrorKey longer than 20 charactersShorten the key
Archive page missinghas_archive is false (the default)Set has_archive to true and flush rules
Meta not in REST responseNo custom-fields support or show_in_rest on the metaAdd both

SEO Settings for Custom Post Types

  • Pick the rewrite slug before launch. Changing it later changes every URL and needs 301 redirects.
  • Decide whether the archive should rank. If it should, give it an intro paragraph; most SEO plugins let you set the archive title and description, and many themes let you add text.
  • Check that your SEO plugin includes the post type and taxonomy in its XML sitemap, and exclude types that are internal only.
  • Use structured data that matches the content. Schema.org has types for people, events, courses and products; projects and case studies usually fit Article or CreativeWork.

If you would rather have a developer plan the content model, eSEOspace builds custom post types as part of WordPress development projects.

Frequently Asked Questions

How do I create a custom post type in WordPress without a plugin?

Write a function that calls register_post_type() with a prefixed key of 20 characters or fewer and an arguments array, hook it to init, and place it in a small custom plugin or must-use plugin. Set public, has_archive, rewrite, supports and show_in_rest, then save Settings > Permalinks once so the new URLs stop returning 404 errors.

Where is the register_post_type() documentation?

The official reference is in WordPress Developer Resources at developer.wordpress.org/reference/functions/register_post_type/. It lists every argument with its default, the 20-character key limit, the reserved post type names such as wp_template, and notes on flushing rewrite rules. The Plugin Handbook's post types chapter adds naming and plugin-versus-theme advice.

What does show_in_rest do in register_post_type?

show_in_rest exposes the post type to the WordPress REST API. It defaults to false. You need it set to true for the block editor to load on that post type, for the Query Loop block and Site Editor templates to see it, and for headless front ends to read the content at /wp-json/wp/v2/ followed by the rest_base.

How do I add a custom taxonomy to a custom post type?

Call register_taxonomy() on init with your taxonomy key, the post type key in the object type array, and arguments such as hierarchical, show_admin_column, show_in_rest and rewrite. Then call register_taxonomy_for_object_type() or list the taxonomy in the post type's taxonomies argument, and flush permalinks so the term archive URLs work.

Why do my custom post type pages show a 404 error?

Usually WordPress has not rebuilt its rewrite rules since you registered the post type or changed its slug. Go to Settings > Permalinks and click Save Changes. If the 404 remains, check that public and publicly_queryable are true and that no page or other post type already uses the same slug.

Get a FREE Website Audit

Enter your website and get a 20–30 page SEO, GEO, AEO & CRO audit within 24 hours. Completely free.

Don't have a site yet? Get in touch →

Ready to speak with a marketing expert?
Give us a call! (916) 866-7893

Websites Launched
1407
Websites Launched
Marketing Experts
29
Marketing Experts
Hours of Dedicated Work
204533
Hours of Dedicated Work
Irina S.Benjamin G.Jeanette M.David G.