What is a WordPress Custom Post Type (CPT)?
By default, WordPress comes with several built-in post types: Posts, Pages, Attachments, Revisions, and Navigation Menus. While these are sufficient for a basic blog, modern websites often require more structured, specialized data.
A Custom Post Type (CPT) allows you to register your own content structures. For example, if you are
building a real estate website, you would create a Property CPT. If you are building a
portfolio, you would create a Project CPT. This keeps your custom content logically
separated from your standard blog posts, making database queries faster and content management much
easier for clients.
Why use a Generator instead of a Plugin?
There are popular plugins like Custom Post Type UI (CPT UI) that allow you to create post
types through the WordPress dashboard. However, professional developers generally prefer writing the
code directly into a plugin or the theme's functions.php file. Here's why:
- Performance: Relying on a heavy UI plugin to register post types adds unnecessary database queries and overhead to every page load. Hardcoded PHP arrays are processed instantly by the server.
- Portability: When you code your CPTs, they travel perfectly with your theme or custom plugin. If you migrate a site or deploy via Git, you don't have to worry about exporting/importing plugin settings.
- Security & Cleanliness: Fewer plugins mean a smaller attack surface and less dashboard clutter for your end-clients.
Crucial Settings Explained
When using our generator, pay attention to these specific flags in the
register_post_type() arguments:
show_in_rest
This is arguably the most important boolean to check in modern WordPress. Setting this to
true enables the WordPress REST API for your post type. More importantly, it
activates the Gutenberg Block Editor for this post type. If you set this to false, your
CPT will fall back to the old, classic TinyMCE editor.
hierarchical
Setting this to true makes your post type behave like a Page, meaning it can have
"Parent" and "Child" posts. Setting it to false makes it behave like a standard Blog
Post, strictly ordered by date.
has_archive
If true, WordPress will automatically create an archive route for your posts (e.g.,
yoursite.com/portfolio/) which displays a loop of all posts in that custom type. You
can then template this using archive-portfolio.php in your theme hierarchy.