Extending WordPress with Custom Post Types: Real-World Examples

19 Nov

WordPress ships with two primary content types: posts and pages. When a site requires a different kind of record, the built-in options fall short.

A custom post type introduces a new container that can hold any data you need, from portfolio items to product listings.

Registering a custom post type

The core function for creating a type is register_post_type. It takes a unique slug and an array of arguments that control labels, capabilities, and the editing interface.

A minimal registration might look like this:

add_action('init', 'register_portfolio_type');
function register_portfolio_type() {
register_post_type('portfolio', [
'labels' => [
'name' => 'Portfolio Items',
'singular_name' => 'Portfolio Item',
],
'public' => true,
'show_in_menu' => true,
'supports' => ['title', 'editor', 'thumbnail'],
]);
}

The slug portfolio becomes part of the URL, while the labels array determines what users see in the admin area. The supports argument lists the meta boxes that appear on the edit screen.

Real-world examples

Portfolio

A portfolio is a common first custom type. It usually includes a title, a description, a featured image, and perhaps a link to a project site. The code above already provides those fields. To make the list view more useful, you can add a custom taxonomy for categories such as “Web Design” or “Photography”.

Event

Events need extra fields like start date, end date, and location. These can be added with custom meta boxes or by using a plugin that extends the type. The type itself can be registered with supports including title, editor, and thumbnail, while the date fields are stored as post meta.

add_action('init', 'register_event_type');
function register_event_type() {
register_post_type('event', [
'labels' => [
'name' => 'Events',
'singular_name' => 'Event',
],
'public' => true,
'show_in_menu' => true,
'supports' => ['title', 'editor', 'thumbnail'],
]);
}

Recipe

Recipes often require ingredients, cooking time, and difficulty level. Instead of embedding that data in the content, you can store each piece as meta. A custom type named recipe can be registered similarly, and then you can add meta boxes using the add_meta_box function.

Product

For an online store, a product type is essential. It should support price, SKU, and stock status. While WooCommerce provides its own product type, you can create a simplified version for a catalog that does not need full e-commerce functionality. The type can be registered with public set to true and show_in_menu enabled.

Testimonial

Testimonials are short quotes that often include the author’s name and position. A custom type called testimonial can include a title for the quote and a meta field for the author. The type can be set to public false if you only want it visible in a custom query on the front end.

Custom taxonomies

Taxonomies group related entries. While WordPress provides categories and tags, you may need a different grouping such as “Genre” for books or “Skill Level” for tutorials. The function register_taxonomy creates a new taxonomy that can be attached to any post type.

add_action('init', 'register_genre_taxonomy');
function register_genre_taxonomy() {
register_taxonomy('genre', ['book'], [
'labels' => [
'name' => 'Genres',
'singular_name' => 'Genre',
],
'hierarchical' => true,
]);
}

In this example, the taxonomy genre is linked to a custom post type book. Setting hierarchical to true creates a tree similar to categories, while false produces a flat tag-like list.

Working with the block editor

Custom post types appear in the block editor by default if the show_in_rest argument is set to true. This flag enables the REST API, which the editor uses to fetch and save data. Most modern registrations include this flag to ensure the type works with the new editing experience.

You can also register custom blocks that appear only for a specific post type. The block registration function accepts a post_type property that restricts the block to the chosen type.

Permalinks and archive pages

When a custom post type is set to public, WordPress automatically generates an archive page that lists all entries of that type. The URL for this archive is typically /post-type-slug/. If you need a different URL structure, you can use the rewrite argument to customize the slug or add a prefix.

For example, to place events under /events/ you would set 'rewrite' => ['slug' => 'events'] in the arguments array.

Common pitfalls

  • Forgetting to set show_in_rest to true, which hides the type from the block editor and the REST API.
  • Using the same slug as an existing post type or taxonomy, causing conflicts.
  • Not flushing rewrite rules after adding a new type, which can result in 404 errors.
  • Over-loading the supports array with features that are not needed, leading to a cluttered edit screen.

Best practices

Keep the registration code in a separate plugin or a dedicated include file. This makes it easier to maintain and prevents conflicts with theme updates.

Use descriptive slugs that match the URL you want. If you plan to have a hierarchy, consider setting hierarchical to true in the type arguments.

When adding meta fields, prefer the register_meta function or a meta box over storing data directly in the content. This keeps the data structured and accessible to other tools.

Test the type on a staging site before deploying. Check that the archive page, the single entry view, and the admin list all behave as expected.

Frequently Asked Questions (FAQs)

Can I create a custom post type without writing code?

Yes. Several plugins such as Custom Post Type UI or Pods allow you to define types through an interface. They generate the necessary PHP code behind the scenes, which can be inspected if you need to adjust it later.

How do I add custom fields to a custom post type?

You can use the add_meta_box function to create a box on the edit screen, or employ register_meta to expose fields via the REST API. Many developers also use plugins like Advanced Custom Fields for a more user-friendly approach.

Will a custom post type affect my site’s SEO?

A custom type can be indexed by search engines if it is set to public and the permalinks are configured correctly. Adding descriptive titles, meta descriptions, and structured data will help improve visibility.

Can I use a custom post type with the Classic editor?

Yes. The classic editor works with any post type that supports the editor entry in the supports array. If you remove that entry, the editor area will not appear.

How do I make a custom post type appear in the admin menu under a custom position?

The show_in_menu argument can be set to a string that matches the slug of another top-level menu, or to a numeric position. For example, 'show_in_menu' => 'tools.php' places the type under the Tools menu.

Is it possible to have a custom post type that is not visible in the admin at all?

Yes. Set show_ui and show_in_menu to false. The type can still be queried programmatically, but it will not appear in the admin area.

What is the difference between a custom post type and a custom taxonomy?

A post type defines a kind of content, similar to a table in a database. A taxonomy categorizes those content items, much like tags or categories. You can attach multiple taxonomies to a single post type.

Leave a Reply

Your email address will not be published. Required fields are marked *