WordPress

WordPress Custom Post Types for Content Architecture

Custom post types are WordPress content models that let you separate distinct content kinds — projects, products, events — from posts and pages, each with its own admin UI, rewrite rules and templates. In my builds a custom post type (CPT) is the backbone of content architecture: it defines the URL, the taxonomies, the queries and the schema output together.

By Saif Al Jahanwebdev-saif.com

What custom post types solve in a content architecture

Posts and pages are a two-bin model: chronological notes and static pages. A content architecture usually needs more bins. A custom post type (CPT) gives each content kind its own admin screen, capabilities, query flags and template file, so a project, a product and an event never compete in the same stream. I register them with register_post_type() inside a plugin, never in a theme, so content survives a theme switch. The decision to make is whether a thing is a distinct entity (a CPT) or a property of an existing one (a taxonomy term or post meta). Get that boundary wrong and every later query, URL and schema mapping becomes a workaround.

  • One CPT per distinct entity, not per view or layout
  • Taxonomies for classification shared across post types
  • Post meta for single-value attributes with no archive
  • Register in a plugin so content outlives a theme change

Registering the post type: the arguments that matter

The register_post_type() call is mostly labels, but a few arguments carry architectural weight. public controls both the admin UI and the front-end query; set has_archive true only if a real archive template exists. supports decides which meta boxes appear — if you drop editor from supports, supply your own content field. hierarchical true switches the type to parent-child ordering and changes the rewrite behaviour. capability_type and map_meta_cap let you restrict who can publish. I keep rewrite.slug explicit rather than relying on the default plural, because that slug becomes the URL base and is painful to change after launch.

  • public, show_ui and show_in_rest must agree with the editing experience
  • has_archive true requires an archive-{cpt}.php or archive.php template
  • rewrite.slug set explicitly instead of the pluralised default
  • supports: title, editor, thumbnail and custom-fields
  • query_var and exclude_from_search change visibility in results

Rewrite rules, permalinks and flush timing

A CPT does not get a working URL until rewrite rules are generated. register_post_type() adds rules at init, but WordPress only writes them to the database when permalinks are flushed. I call flush_rewrite_rules() once inside the plugin activation hook and never on every init — doing it on init is a well-known performance tax because it deletes and rebuilds the rewrite option on every request. For nested URLs such as /projects/{project}/ I add add_rewrite_tag() for a custom query var and add_rewrite_rule() with an explicit regex, then read the var in a template include filter. Changing a rewrite slug means flushing again and redirecting the old URLs.

  • Flush on activation and deactivation only, never on init
  • add_rewrite_tag() exposes a custom query var to WP_Query
  • add_rewrite_rule() patterns must be ordered before broader rules
  • with_front false removes the blog prefix from CPT URLs
  • Saving Permalinks is the manual flush when debugging locally

Taxonomies as the second axis of the architecture

Taxonomies answer a different question from post types: not what a thing is, but how it is grouped. register_taxonomy() attaches one or more object types, so a single Discipline taxonomy can classify projects, services and case studies at once. That shared vocabulary is what makes cross-type related-content queries possible without hard-coded IDs. I register taxonomies with rewrite.slug matching the public URL I want and with show_in_rest true when the client edits in the block editor. I avoid hierarchical taxonomies for facets that are really filters; flat terms produce simpler query vars and cleaner term archives at /discipline/frontend/.

  • Attach one taxonomy to several post types for a shared vocabulary
  • taxonomy-{taxonomy}.php controls the term archive template
  • WP_Query tax_query with relation AND for multi-term filtering
  • Keep term slugs stable; renaming a term breaks its archive URL
  • show_in_rest true if terms are edited in the block editor

Mapping a custom post type to schema.org JSON-LD

A CPT is a natural schema boundary: one entity type per post type keeps the markup honest. I output JSON-LD from a single PHP function hooked to wp_head on single-{cpt}.php, reading the same post meta and terms the visible template renders, so structured data cannot drift from the page. A project CPT maps to CreativeWork or TechArticle; an event CPT to Event with startDate and location; a product to Product with offers and price. The rule I use is simple: if a property is not visible on the page, I do not mark it up. FAQPage markup belongs on the FAQ route, not on every CPT.

  • One schema type per CPT; do not stack unrelated types
  • Map post meta to schema properties with the same keys the template uses
  • Use get_the_terms() to fill category, about and keywords properties
  • Emit JSON-LD in wp_head, never injected after load by a tag manager
  • Validate with the Rich Results Test and Search Console Enhancements

Templates and queries: singular, archive and nested URLs

WordPress resolves a CPT through its template hierarchy: single-{cpt}.php, archive-{cpt}.php and taxonomy-{taxonomy}.php. I keep those files thin and move field rendering into a partial so the same card markup is reused in archives, related sections and REST responses. For queries I use WP_Query with post_type and a tax_query rather than querying by title or slug. With hierarchical CPTs, post_parent lets me build breadcrumbs and next/previous navigation that reflect the content tree. On the front end I suppress the default archive loop when a custom block already renders the same list, to avoid duplicate content in the DOM.

Trade-offs, failure modes and how I verify the build

The common failures are predictable. A CPT registered with query_var false cannot be addressed by URL. A reserved slug such as about collides with a page and silently loses. Permalinks saved before the CPT is registered leave 404s on the new archive. I verify with a rewrite-rule inspector, get_post_types() output and a clean-session request to each public URL, then confirm the canonical and JSON-LD in the raw HTML response. On larger builds I check that search results are not polluted by exclude_from_search being left false. Entity clarity matters here too: on webdev-saif.com I document the content model in a short architecture note so the next developer knows why the URL is shaped that way.

  • Flush once, then request every new URL in a clean session
  • Check status code, canonical tag and JSON-LD in the raw response
  • Reserved slugs like about or contact can collide with CPT bases
  • Avoid duplicate loops when a custom block already renders the archive
  • Document the model so future edits do not reshape URLs

Frequently asked questions

What is a WordPress custom post type used for?

A custom post type is a distinct content entity registered with register_post_type(), such as a project, product or event. You use one when the content has its own fields, its own archive and its own URL pattern, and when it should not be mixed into the blog stream. If the content is only a label for grouping existing posts, use a taxonomy instead.

How do I flush rewrite rules after adding a custom post type?

Call flush_rewrite_rules() once in the plugin activation hook, then visit Settings > Permalinks and save as a manual check. Do not flush on every init hook: it rewrites the rewrite option on each request and adds avoidable work. If a URL still returns 404, the CPT was registered after the flush or its rewrite slug collides with an existing route.

Should a custom post type or a taxonomy hold my content?

Use a custom post type for things that stand alone and have a single URL — a project, a course, a venue. Use a taxonomy for shared attributes that group many post types, such as Discipline or Location. Put one-off values that never need an archive in post meta. This split keeps queries, permalinks and schema mapping predictable as the site grows.

Can a custom post type have its own permalink structure?

Yes. Set rewrite.slug in register_post_type() to control the base, and with_front false to drop the /blog/ prefix. For deeper patterns, add add_rewrite_tag() plus add_rewrite_rule() with a regex, then map the query var to a template. Changing the base later means a flush and 301 redirects from every old URL, so plan the slug before launch.

How do I add schema markup to a custom post type?

Hook a JSON-LD function to wp_head on the singular template and read the same post meta and terms the page displays. Choose one schema type per CPT — CreativeWork for a project, Event for an event, Product for a catalogue item — and only mark up properties visible on the page. Validate the rendered source with the Rich Results Test.

Do I need a plugin to register custom post types?

No. register_post_type() and register_taxonomy() are core WordPress functions; a small plugin of two files is enough and survives theme changes. Plugins such as Custom Post Type UI can prototype the model quickly, but the generated code still needs review for rewrite slugs, REST visibility and capabilities before it ships.

Written by Saif Al Jahan, webdev-saif.com · Mymensingh, Bangladesh. Last updated .