Real-world WordPress websites almost always outgrow default blog posts and static pages. Whether you are building Portfolios, Case Studies, Testimonials, or Documentation Libraries, registering Custom Post Types and Taxonomies in clean PHP keeps your data structured, fast, and independent of bloated admin plugins.
1. Registering Custom Post Types and Taxonomies via register_post_type()
Per the WordPress register_post_type() Reference, hook registration into init, keep post type keys within WordPress’s 20-character lowercase limit, and always enable 'show_in_rest' => true so the Gutenberg block editor and /wp-json/wp/v2/ REST API endpoints work seamlessly:
function codetowp_register_portfolio_cpt() {
$labels = array(
'name' => _x( 'Portfolios', 'Post Type General Name', 'codetowp' ),
'singular_name' => _x( 'Portfolio', 'Post Type Singular Name', 'codetowp' ),
'add_new_item' => __( 'Add New Project', 'codetowp' ),
'edit_item' => __( 'Edit Project', 'codetowp' ),
);
register_post_type( 'portfolio', array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'menu_icon' => 'dashicons-portfolio',
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
'rewrite' => array( 'slug' => 'portfolio' ),
) );
}
add_action( 'init', 'codetowp_register_portfolio_cpt' );
2. Attaching Custom Taxonomies
To organize entries into structured groups, register a companion taxonomy on init via register_taxonomy(). Set 'hierarchical' => true for category-style parent/child selection or false for flat tags, keeping 'show_in_rest' => true enabled so the taxonomy panel appears in the block editor sidebar:
function codetowp_register_portfolio_tax() {
register_taxonomy( 'portfolio_category', array( 'portfolio' ), array(
'hierarchical' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'rewrite' => array( 'slug' => 'portfolio-category' ),
) );
}
add_action( 'init', 'codetowp_register_portfolio_tax' );
3. Theme vs. Companion Plugin, Templates & Permalinks
- Where CPT Code Should Live: Place CPT definitions inside a site-specific companion plugin rather than
functions.phpwhenever possible so switching themes never hides your client’s portfolio or testimonials. - Dedicated Template Hierarchy: Create
single-{post_type}.phpandarchive-{post_type}.phpin your theme when custom layouts are needed; otherwise WordPress falls back cleanly tosingle.phpandarchive.php. - Fixing 404 Errors: Whenever you register a new CPT slug, open Settings > Permalinks in WordPress Admin and click Save Changes to flush cached rewrite rules.
4. Controlling REST API Exposure, Search & Archive Queries
When registering Custom Post Types and Taxonomies in production sites, explicitly configure search visibility and archive ordering rather than relying solely on defaults. Set 'exclude_from_search' => true on internal types (such as reusable hero slides or testimonial snippets) so thin records do not clutter public WordPress search results, and set 'publicly_queryable' => false when entries should never expose standalone permalink URLs.
For public archives paired with Custom Post Types and Taxonomies—such as Case Studies, Portfolios, or Documentation Hubs—register clean rewrite slugs, declare clear supports arrays (such as revisions and custom-fields), and attach targeted pre_get_posts filters in PHP to control sorting order without writing custom SQL.
Frequently Asked Questions
Why can a post type key only be 20 characters long?
The post_type database column in wp_posts is defined as varchar(20), so registering a key over 20 characters triggers an invalid post type notice or silent failure in WordPress core.
Should I call flush_rewrite_rules() inside init?
Never call flush_rewrite_rules() inside init because rebuilding rewrite rules on every page request hurts database performance. Only flush on plugin activation or once manually via Settings > Permalinks.
How do I add custom metadata fields cleanly alongside a CPT?
Register native meta boxes with wp_nonce_field() verification and sanitized save_post_{post_type} callbacks—or generate both the CPT and custom fields visually using our CPT Generator.
Can Custom Post Types and Taxonomies appear inside the main blog feed?
Yes—by default the main blog query displays standard posts only, so if you want a custom post type like case studies to appear alongside articles on category archives or the homepage loop, attach a targeted pre_get_posts hook checking is_main_query() without writing custom SQL.
Generate complete CPT, taxonomy, and nonce-protected meta box boilerplate instantly using our WordPress CPT & Taxonomy Code Generator, and build custom layouts for your post types with the Elementor Widget Builder or Starter Theme Generator.