How to Create Custom Post Types in WordPress (With Code)
Register a custom post type with register_post_type: labels, supports, rewrite slugs, REST and block editor support, archive templates, capabilities and common pitfalls.

To create a custom post type in WordPress, call register_post_type() on the init hook from a plugin, give it labels and arguments such as public, has_archive, supports and show_in_rest, then visit Settings > Permalinks once so WordPress rebuilds its URLs. The new type gets its own admin menu, URLs, archive and templates, and you can query it like any post.
Custom post types (CPTs) are how WordPress handles content that is not a blog post or a page: projects, case studies, team members, events, products, courses. This guide gives you complete, production-ready code and explains each option.
When to use a custom post type
Use a CPT when content has its own structure, its own listing page or its own editing workflow. Use a custom taxonomy instead when you only need to group existing content, and custom fields when you only need extra data on existing content.
| You need | Use |
|---|---|
| A new kind of content with its own URLs and admin screen | Custom post type |
| To group content (by industry, location, level) | Custom taxonomy |
| Extra data on each item (price, date, client) | Custom fields / post meta |
Step 1: Put the code in a plugin, not the theme
If you register a CPT in your theme's functions.php, switching themes makes all that content vanish from the admin. Register it in a small plugin instead. Create wp-content/plugins/tle-projects/tle-projects.php:
<?php
/**
* Plugin Name: TLE Projects
* Description: Registers the Project post type.
* Version: 1.0.0
*/
defined( 'ABSPATH' ) || exit;
New to plugins? See how to create a custom WordPress plugin.
Step 2: Register the post type
add_action( 'init', 'tle_register_project_cpt' );
function tle_register_project_cpt() {
$labels = array(
'name' => __( 'Projects', 'tle-projects' ),
'singular_name' => __( 'Project', 'tle-projects' ),
'add_new_item' => __( 'Add New Project', 'tle-projects' ),
'edit_item' => __( 'Edit Project', 'tle-projects' ),
'view_item' => __( 'View Project', 'tle-projects' ),
'all_items' => __( 'All Projects', 'tle-projects' ),
'search_items' => __( 'Search Projects', 'tle-projects' ),
'not_found' => __( 'No projects found.', 'tle-projects' ),
'menu_name' => __( 'Projects', 'tle-projects' ),
);
register_post_type( 'tle_project', array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'projects', 'with_front' => false ),
'menu_icon' => 'dashicons-portfolio',
'menu_position' => 20,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'custom-fields' ),
'show_in_rest' => true, // required for the block editor and the REST API
'taxonomies' => array(),
) );
}
Step 3: Flush permalinks
New rewrite rules are not active until WordPress rebuilds them. Go to Settings > Permalinks and click Save Changes. In a plugin, do it automatically on activation:
register_activation_hook( __FILE__, function () {
tle_register_project_cpt();
flush_rewrite_rules();
} );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
Never call flush_rewrite_rules() on every page load; it is slow and writes to the database.
The arguments that matter
| Argument | What it does | Tip |
|---|---|---|
public | Sets sensible defaults for front-end visibility, search and admin UI. | Set to false for internal data, then enable show_ui if editors need it. |
has_archive | Creates a listing page, e.g. /projects/. | Pass a string to use a different archive slug. |
rewrite | Controls the URL base. | Use a plural, human slug; set with_front to false if your permalinks start with /blog/. |
supports | Which editor features appear. | Add thumbnail for featured images and custom-fields for meta in REST. |
show_in_rest | Exposes the type to the REST API. | Without it the block editor is not used and the classic editor appears. |
hierarchical | Parent/child items like pages. | Leave false unless you really need nesting. |
capability_type / map_meta_cap | Custom permissions. | See below. |
template | Default blocks for new items. | Great for consistent layouts. |
The post type key (tle_project) must be 20 characters or fewer, lowercase, and should be prefixed so it never clashes with another plugin. Avoid reserved names like post, page, action or order.
Step 4: Templates for the new type
WordPress looks for these files in your theme, following the template hierarchy:
single-tle_project.phpfor a single project;archive-tle_project.phpfor the/projects/listing.
In a block theme, create the templates in the Site Editor (Appearance > Editor > Templates > Add New) or as templates/single-tle_project.html. If none exists, WordPress falls back to single.php / archive.php / index.php.
Step 5: Query your custom posts
$projects = new WP_Query( array(
'post_type' => 'tle_project',
'posts_per_page' => 6,
'no_found_rows' => true, // skip pagination count when you do not need it
) );
while ( $projects->have_posts() ) {
$projects->the_post();
the_title( '<h3>', '</h3>' );
}
wp_reset_postdata();
To change the main archive query (number per page, order), use pre_get_posts rather than a second query. See the WP_Query guide and pre_get_posts vs WP_Query.
Custom capabilities (optional)
By default, anyone who can edit posts can edit projects. To control them separately:
'capability_type' => array( 'project', 'projects' ),
'map_meta_cap' => true,
Then grant capabilities such as edit_projects and publish_projects to the right roles on plugin activation. See roles and capabilities. If you switch this on and grant nothing, even administrators lose access.
Common mistakes
- 404 on the new URLs: permalinks were not flushed.
- Classic editor appears:
show_in_restis missing. - Featured image box missing: add
thumbnailtosupportsand make sure the theme declaresadd_theme_support( 'post-thumbnails' ). - Slug clash: a page called "Projects" and a CPT archive at
/projects/compete for the same URL. Rename one. - Registered too late: register on
init, not inside a template or a later hook. - Changing the post type key later: existing items keep the old key in the database and disappear from the admin. Pick the key once.
SEO notes for custom post types
A public CPT appears in the WordPress core sitemap (/wp-sitemap.xml) and in SEO plugin sitemaps automatically. Give the archive a real introduction and title, and decide whether thin types (for example testimonials) should be public at all. See optimizing XML sitemaps.
Frequently asked questions
Should I use a plugin like CPT UI or write code?
Both register the same thing. A UI plugin is fine for simple sites; code in your own plugin is version-controlled, portable and has no extra dependency.
Why does my custom post type return a 404?
The rewrite rules have not been rebuilt. Visit Settings > Permalinks and click Save, or flush on plugin activation.
How do I add custom fields to a custom post type?
Register them with register_post_meta() for block editor and REST support, add a meta box, or use a fields plugin. See custom meta boxes.
Will my content disappear if I deactivate the plugin?
No. The posts stay in wp_posts; they simply become inaccessible until the type is registered again.
Conclusion
A custom post type is one function call with a handful of well-chosen arguments, but placing it in a plugin, flushing rewrites, enabling REST and planning templates is what makes it robust. Next, add taxonomies to organize it. Reference: register_post_type() and the Plugin Handbook on post types.
Planning a custom content model for a larger site? Talk to our WordPress developers.
Save this practical guide
Add us as a preferred Google source, or ask an AI assistant to summarize this page and link back to the original guide when it is useful.
