How to Create a Custom Post Type in WordPress

How to Create a Custom Post Type in WordPress

WordPress ships with posts and pages, but real sites need more: products, events, portfolios, testimonials. Custom post types (CPTs) give each of those its own admin menu, editor, and URLs. Here is how to add one properly.

What a custom post type is

A CPT is a content type you register with WordPress. Once registered, it gets its own “Add New” screen, its own list table, and (if you want) its own archive and single-post URLs. The function that creates one is register_post_type(), called on the init hook.

Generate the code

  1. Open the Custom Post Type Generator.
  2. Set the key (the internal slug, 20 characters or fewer), the singular and plural labels, and pick your options.
  3. Copy the generated code into your theme’s functions.php or a small plugin.

The tool writes the full label set for you, which is the tedious part to type by hand.

The options that matter

  • public: whether the type is visible on the front end and in the admin. Usually true.
  • show_in_rest: must be true to use the block editor (Gutenberg) and the REST API.
  • has_archive: enables an archive page at /your-slug/.
  • hierarchical: true makes it behave like pages (with parents); false like posts.
  • supports: which editor features appear (title, editor, thumbnail, excerpt, custom-fields, and more).
  • menu_icon: a Dashicon for the admin menu.
  • rewrite: the URL slug for single items.

Do not forget to flush rewrite rules

After adding a CPT with a custom slug, its URLs will 404 until rewrite rules are refreshed. The simplest fix: visit Settings then Permalinks once and click Save. You do not need to change anything; just loading that page flushes the rules.

Put it in a plugin, not just the theme

If the content should survive a theme switch (it usually should), register the CPT in a small site-specific plugin rather than functions.php. That way changing themes never makes your products or events disappear from the admin.

Pick a key, set the labels, choose your options, and flush permalinks. Your new content type is ready in minutes.

← All posts