designwithfocus

WordPress & CMS

What Is a Custom Post Type? WordPress Beyond Posts

By Design With Focus Team 5 min read
A WordPress admin menu with a custom content type beside Posts

Short answer

A custom post type is a content type you register in WordPress beyond the built-in ones, such as posts and pages. Every type is stored in the wp_posts table and separated by the post_type column. The plugin handbook says the identifier must be 20 characters or fewer. You register it with register_post_type on the init hook, preferably from a plugin so the content survives a theme change.

Last verified against the sources listed below.

Key takeaways

  • WordPress stores every post type in wp_posts and tells them apart with the post_type column.
  • The theme handbook lists post, page, attachment, revision, nav_menu_item, wp_template, and wp_template_part.
  • register_post_type adds your own type. Call it on init, from a plugin rather than a theme.
  • The identifier must be 20 characters or fewer. The database column is varchar(20).
  • Do not start the identifier with wp_. A generic name such as product can collide with another plugin.
  • Posts are timed and feed-based. Pages are hierarchical. A custom type picks its own supports and rewrite slug.

What is a custom post type is a WordPress content type you add beside the ones the software already ships. The plugin handbook says you register it when posts and pages are the wrong shape for the content, such as products, assignments, or movies.

All of those types share one table. The theme handbook says they are stored in wp_posts and separated by a column named post_type. A custom type is another value in that column, plus the admin screen and URLs you turn on when you register it.

Which post types does WordPress already have?

The theme handbook, updated February 13, 2023, lists seven default types. The plugin handbook’s registration chapter, updated November 17, 2022, names five: post, page, attachment, revision, and menu. The later theme list is the one to match in code, because it includes the internal names.

Type name in the theme handbook What that handbook says it is for
post Blog entries, newest first, with a date, categories, tags, and feeds
page Static pages outside the blog stream, able to be parents and children
attachment Uploaded media, with image data stored in post meta
revision A built-in type the handbook lists but does not recommend removing
nav_menu_item Navigation menu entries
wp_template Block templates
wp_template_part Template parts

The handbook says a plugin or theme can modify these, and it advises widely distributed code not to remove built-in behavior. The plugin handbook’s word menu is not the same string as nav_menu_item. Use the name the theme handbook prints when you are checking the database.

How do you register one?

You call register_post_type(). The plugin handbook’s minimal example registers wporg_product on the init hook, with a plural label Products, a singular label Product, public set to true, and has_archive set to true.

The call has to happen before admin_init and after after_setup_theme. init is the hook the handbook recommends. Once the type is public, it gets a top-level admin screen for creating and managing those entries.

Learn WordPress describes development as jumping into the code to extend the software. Registration is that kind of extension. It is PHP you add, not a setting on the Posts screen.

How long can the identifier be?

20 characters is the limit. The plugin handbook says the identifier must not exceed 20 characters because the post_type column is a VARCHAR of that length. The schema in wordpress-develop shows the column as varchar(20), not null, with a default of post.

Prefix the identifier with a short string for your plugin, theme, or site. Do not start it with wp_. The handbook says core uses that prefix. A generic identifier such as product can collide with another plugin, and the handbook says you cannot fix a duplicate identifier without disabling one of the types.

What URL does a custom type get?

By default the slug is the identifier. The handbook’s example is http://example.com/wporg_product/wporg-is-awesome. wporg_product is the type slug, and the last segment is the individual entry.

A rewrite argument can change the slug. Setting it to products yields http://example.com/products/wporg-is-awesome. A generic slug can also collide. Unlike the identifier, the handbook says a slug clash is fixed by changing one of the slugs.

The function reference says hierarchical defaults to false, so a new type behaves more like posts than like nested pages unless you turn that flag on. public influences whether the type is excluded from search, queryable, and shown in the admin. You can override each of those flags on its own.

How is a type different from a category?

A category files posts that already exist. The categories screen says each post is filed under one or more categories, and a category can have a parent. That is organization inside the post type.

A custom post type is a new kind of row. The theme handbook says posts are time-based and used for feeds, while pages are not organized with categories and tags and can form a parent-child tree. Your type picks its own features through the supports argument. The reference lists title, editor, comments, revisions, author, excerpt, thumbnail, and custom-fields among them.

Where should the code live?

Put the registration in a plugin. Both handbooks say a theme is the wrong home if you want the content to stay portable. Change the theme, and a type registered only there disappears from the admin.

The theme can still display a type a plugin registered. The theme handbook names single-{post-type} and archive-{post-type}, plus search and index. That is presentation. The type itself stays with the plugin.

A site that publishes more than posts still needs the small identity pieces around the pages. The favicon generator makes the icon for that site.

How the templates and the editor fit together is what WordPress is used for.

The layout of those templates is web design.

Frequently asked questions

Are posts and pages both post types?

Yes. The theme handbook says post type is the general name for kinds of content, which is confusing because a post is also one specific type. A post uses the type name post. A page uses page. Both rows live in wp_posts. Posts are shown in reverse time order, can use categories and tags, and are used for feeds. Pages are outside that stream, are not organized with those taxonomies, and can be parents and children of other pages.

How long can a custom post type name be?

The plugin handbook says the identifier must not exceed 20 characters, because the post_type column is a VARCHAR of that length. The WordPress schema in the develop repository matches that: post_type is varchar(20), not null, and defaults to post. Prefix the name with something short that belongs to your plugin. Do not use wp_ at the start. WordPress core uses that prefix, and the handbook says to leave it alone for forward compatibility.

Should a custom post type live in a theme?

The plugin handbook and the theme handbook both say no for a type you expect to keep. Put the registration in a plugin. If the code is only in the theme, switching themes removes the admin screens for that type, and the content is no longer managed the way you set up. The theme can still supply single and archive templates for a type a plugin registered. The handbook names single-{post-type} and archive-{post-type} for that display work.

What is the difference between a category and a post type?

A category groups posts that already share the post type. WordPress.org's categories screen says each post is filed under one or more categories, and categories can have parents. A custom post type is a different kind of entry, with its own editor screen, rewrite slug, and supports list. Products, assignments, and movies are the handbook's examples of types, not of categories. You can still attach a taxonomy to a custom type, but the type itself is not a category.

When should register_post_type run?

The plugin handbook says to call register_post_type before admin_init and after after_setup_theme. The init action is the hook it recommends. A minimal registration sets a labels array, public to true, and has_archive to true. Add a rewrite slug if the default URL, which uses the type identifier, is too long or too generic. A generic slug such as products can collide, and unlike the identifier, a slug clash can be fixed by changing one slug.

Sources