A WordPress custom post type is a content type you define yourself, alongside the built-in posts and pages. It gives a specific kind of content, such as projects, events, team members or properties, its own admin menu, its own fields and its own URLs and templates.
Out of the box, WordPress stores everything as posts, pages, media and a few internal types. That works until you try to squeeze a portfolio, a job board or a product catalog into blog posts with categories. This guide explains what custom post types are, when to use one, and how to register one properly in a small plugin, including taxonomies, block editor support and templates.
Quick answer: To create a WordPress custom post type, call register_post_type() on the init hook with a unique key (20 characters or fewer) and an array of arguments such as labels, public, supports, show_in_rest and rewrite. Put the code in a small plugin rather than your theme so the content survives a theme change, and flush rewrite rules on activation so the new URLs work.
What is a WordPress custom post type?
Internally, every piece of content in WordPress is a row in the posts table with a post_type column. Blog posts have the type post, pages have page, and uploaded files have attachment. A custom post type simply adds a new value to that column, plus rules for how WordPress should treat it in the admin, the REST API and the front end.
Because they use the same table and the same functions, custom post types get most WordPress features for free: the editor, revisions, featured images, search, custom fields, REST endpoints and theme templates. You decide which of those to turn on.
When you need a custom post type
A custom post type makes sense when content has its own structure and its own purpose, separate from your blog. Typical examples:
- A portfolio of client projects with a client name, services and a gallery.
- Events with dates, venues and ticket links.
- Team members or staff profiles.
- Real estate listings, job openings or courses.
- Testimonials or case studies you want to display in several places.
If the content is just another kind of article, a category or tag is usually enough. Ask whether it needs different fields, a different URL structure or a different template. If the answer is yes to two of those, a custom post type is likely the better fit.
Custom post type vs category vs custom field
| Tool | Use it for | Example |
|---|---|---|
| Custom post type | A distinct kind of content with its own screens and templates | Projects, Events, Team |
| Taxonomy (category, tag or custom) | Grouping items of one type | Project type: Branding, Web, Print |
| Custom field (post meta) | Single pieces of data on one item | Client name, event date, price |
Most real builds use all three together: a Project post type, a Project Type taxonomy to group projects, and custom fields for details like the client name.
How to create a WordPress custom post type in a plugin
You can register post types from a theme’s functions.php file, but if you switch themes later the content disappears from the admin (it is still in the database, just unregistered). A tiny plugin keeps your content independent of the design. If you have never built one, our guide on how to make a WordPress plugin covers the basics.
- In
wp-content/plugins, create a folder calledtnc-projects. - Inside it, create a file called
tnc-projects.php. - Paste the code below, save, then activate the plugin under Plugins in the admin.
- Visit Settings, then Permalinks, if the project URLs return a 404 error. Simply loading that screen refreshes the rewrite rules.
<?php
/**
* Plugin Name: TNC Projects
* Description: Adds a Project custom post type and a Project Type taxonomy.
* Version: 1.0.0
* Requires at least: 6.0
* Requires PHP: 8.0
* Text Domain: tnc-projects
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Register the Project post type.
*/
function tnc_register_project_post_type() {
$labels = array(
'name' => _x( 'Projects', 'post type general name', 'tnc-projects' ),
'singular_name' => _x( 'Project', 'post type singular name', 'tnc-projects' ),
'menu_name' => __( 'Projects', 'tnc-projects' ),
'add_new_item' => __( 'Add New Project', 'tnc-projects' ),
'edit_item' => __( 'Edit Project', 'tnc-projects' ),
'new_item' => __( 'New Project', 'tnc-projects' ),
'view_item' => __( 'View Project', 'tnc-projects' ),
'all_items' => __( 'All Projects', 'tnc-projects' ),
'search_items' => __( 'Search Projects', 'tnc-projects' ),
'not_found' => __( 'No projects found.', 'tnc-projects' ),
'not_found_in_trash' => __( 'No projects found in Trash.', 'tnc-projects' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'rewrite' => array(
'slug' => 'projects',
'with_front' => false,
),
'show_in_rest' => true,
'menu_icon' => 'dashicons-portfolio',
'menu_position' => 20,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions' ),
);
register_post_type( 'tnc_project', $args );
}
add_action( 'init', 'tnc_register_project_post_type' );
/**
* Register the Project Type taxonomy.
*/
function tnc_register_project_type_taxonomy() {
$labels = array(
'name' => _x( 'Project Types', 'taxonomy general name', 'tnc-projects' ),
'singular_name' => _x( 'Project Type', 'taxonomy singular name', 'tnc-projects' ),
'all_items' => __( 'All Project Types', 'tnc-projects' ),
'edit_item' => __( 'Edit Project Type', 'tnc-projects' ),
'add_new_item' => __( 'Add New Project Type', 'tnc-projects' ),
);
register_taxonomy(
'tnc_project_type',
array( 'tnc_project' ),
array(
'labels' => $labels,
'hierarchical' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'rewrite' => array( 'slug' => 'project-type' ),
)
);
}
add_action( 'init', 'tnc_register_project_type_taxonomy' );
/**
* Register everything and refresh permalinks on activation.
*/
function tnc_projects_activate() {
tnc_register_project_post_type();
tnc_register_project_type_taxonomy();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'tnc_projects_activate' );
/**
* Clean up rewrite rules on deactivation.
*/
function tnc_projects_deactivate() {
unregister_post_type( 'tnc_project' );
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'tnc_projects_deactivate' );
After activation you will see a Projects menu in the admin. Single projects live at /projects/project-name/, the archive at /projects/, and project types at /project-type/branding/.
Understanding the register_post_type arguments
The function accepts dozens of arguments, all documented in the register_post_type reference. These are the ones you will set on almost every build.
The post type key
The first parameter, tnc_project, is the internal name stored in the database. It must be 20 characters or fewer, use lowercase letters, numbers, dashes or underscores, and should carry a prefix so it does not clash with other plugins. Avoid reserved names such as post, page, attachment, revision, nav_menu_item, action, author, order and theme. Pick it carefully, because changing it later means updating every stored item.
labels
Labels control every piece of admin text: menu names, button text, empty states. If you only set name and singular_name, WordPress falls back to generic wording like “Add New Post” in places. Wrapping labels in translation functions keeps the plugin ready for other languages.
public and has_archive
public set to true makes the post type visible in the admin, on the front end and in search. Set it to false for internal data, such as form entries, that should never have public URLs. has_archive creates a listing page of all items. When it is true, the archive uses the rewrite slug, so here it lives at /projects/.
supports
This array decides which editing features appear: title, editor, thumbnail (featured image), excerpt, custom-fields, revisions, author, comments and page-attributes. Turn on only what the content needs. Leaving out comments, for example, removes the comments box from every project.
show_in_rest
Setting show_in_rest to true does two things: it exposes the post type at /wp-json/wp/v2/tnc_project and it enables the block editor. Without it, the post type falls back to the classic editor and will not appear in the Query Loop block, so in practice you want it on for any public content.
rewrite
The rewrite array sets the URL base. slug is what appears in the address bar, and with_front set to false stops WordPress from adding a prefix such as /blog/ if your post permalinks use one. Whenever you change the rewrite slug, flush the rewrite rules by visiting Settings, then Permalinks.
Why flush rewrite rules only on activation
Rewrite rules are expensive to rebuild, so never call flush_rewrite_rules() on every page load. The activation hook above registers the post type first, then flushes once. The deactivation hook unregisters it and flushes again so old project URLs stop resolving cleanly.
Templates for your custom post type
WordPress chooses a template using the template hierarchy, and custom post types slot right into it.
Classic themes
In a classic or child theme, create these files and WordPress will use them automatically:
single-tnc_project.phpfor individual projects.archive-tnc_project.phpfor the /projects/ listing.taxonomy-tnc_project_type.phpfor project type archives.
A minimal single template looks like this:
<?php
/**
* Template for single Project posts.
*/
get_header();
while ( have_posts() ) :
the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1><?php the_title(); ?></h1>
<?php the_post_thumbnail( 'large' ); ?>
<?php the_terms( get_the_ID(), 'tnc_project_type', '<p>Type: ', ', ', '</p>' ); ?>
<?php the_content(); ?>
</article>
<?php
endwhile;
get_footer();
Block themes
In a block theme, templates are HTML files in the theme’s templates folder, so the equivalents are single-tnc_project.html and archive-tnc_project.html. You can also create them without code: go to Appearance, then Editor, open Templates, choose Add New Template, and pick the single item or archive option for Projects. To list projects on any page, add a Query Loop block and set its post type to Projects in the block settings.
Plugins vs code, and getting help
Plugins such as Custom Post Type UI, or recent versions of Advanced Custom Fields, can register post types and taxonomies through a settings screen. They are handy for prototypes and for teams without a developer, but the post type then depends on that plugin staying installed. Code in your own small plugin has no such dependency and can live in version control alongside the rest of the site.
For larger builds, such as a directory with front-end submissions, custom fields with validation, member-only access or an integration with an external API, the post type is only the first step. ThemeNcode’s custom WordPress plugin development service builds this kind of functionality as a clean, maintainable plugin. If you want to control who can edit each post type, our guide to WordPress user roles explains how capabilities work.
Frequently asked questions
Will I lose content if I remove the custom post type code?
No. The items stay in the database, but WordPress hides them in the admin and their URLs stop working. Register the post type again with the same key and everything reappears.
Should custom post types go in the theme or a plugin?
A plugin is almost always better, because content should not depend on the design. Keep only presentation, such as templates, in the theme.
Why do my custom post type pages show a 404 error?
The rewrite rules have not been refreshed. Visit Settings, then Permalinks in the admin, which flushes them, and make sure the post type is public.
Can I add custom fields to a custom post type?
Yes. Include custom-fields in the supports array for basic meta, use register_post_meta() to expose fields to the REST API, or use a fields plugin for a friendlier editing interface.
How do I show custom post types on the homepage or in a grid?
In a block theme, use the Query Loop block and choose your post type. In code, run a WP_Query with the post_type argument set to your post type key.