WordPress Development

Custom Post Types and Custom Fields: Building Structured Content in WordPress

Custom Post Types and Custom Fields: Building Structured Content in WordPress

A lot of WordPress sites I inherit have the same problem hiding under the surface. The business has team members, case studies, products, locations or events, and all of it is stuffed into regular posts or pages. Categories are doing jobs they were never meant for, and editors follow a long list of rules like “always put the price in bold in the second paragraph”.

It works, sort of, until someone wants to filter properties by price, show events in date order, or display the same team member on three pages. Then you realise the data was never really data. It was text.

Custom post types and custom fields fix this. They let you tell WordPress that a “property” is its own kind of thing, with its own fields like price, bedrooms and location. Once content is structured, everything else becomes easier: templates, search, filtering, schema markup and even feeding content to other systems.

Posts, pages and everything else

WordPress already uses post types internally. Posts and pages are the two you know, but menu items, media attachments and reusable blocks are post types too. A custom post type is simply a new entry in that list, designed for your content.

Good candidates for a custom post type share a few traits:

  • There are many of them, not just one or two.
  • Each item follows the same structure.
  • They need their own listing, archive or filtering.
  • They don’t belong in the blog’s chronological flow.

Team members, testimonials, case studies, events, job openings, properties, recipes and courses are all common examples.

Registering a custom post type

You can register post types with a plugin, but the code is short and understanding it is worthwhile. Here’s a properly configured post type for case studies:

A few of these settings deserve explanation.

show_in_rest is essential if you want the block editor. Without it, your post type falls back to the classic editor, and it won’t be available through the REST API either. I’ve seen developers spend an hour wondering why Gutenberg won’t load, and this is nearly always why.

has_archive creates a listing page at the rewrite slug, in this case /case-studies/. If you’d rather build that listing as a normal page, set it to false.

supports controls which features the edit screen shows. Leave out what you don’t need. A testimonial probably doesn’t need comments or excerpts.

Put it in a plugin, not your theme

This is the most common mistake I see, and it causes real pain later. If you register your post types in the theme’s functions.php, your content disappears from the admin the moment you switch themes. The data is still in the database, but WordPress no longer knows what it is.

Content structure belongs to the site, not the design. Put post type registration in a small custom plugin, or a must-use plugin in wp-content/mu-plugins, so it survives any redesign.

Flush rewrite rules, once

After registering a new post type, its URLs often return a 404 at first. That’s because WordPress caches its URL rules. Visiting Settings, then Permalinks, and clicking Save rebuilds them.

In a plugin, do it on activation, not on every page load:

Calling flush_rewrite_rules on every request is an expensive mistake I still find in production code.

Custom taxonomies for grouping

Categories and tags are taxonomies. You can create your own to group custom post types in ways that make sense for your content. Case studies might be grouped by industry, properties by area, courses by level.

Hierarchical taxonomies behave like categories, with parents and checkboxes. Non-hierarchical ones behave like tags. Use taxonomies for anything you’ll filter or group by. Use custom fields for individual values like price or date.

Custom fields for the details

Custom fields store extra values attached to each post: an event date, a property price, a person’s job title. WordPress stores them in the postmeta table as key-value pairs.

Registering meta properly

If you want fields available in the block editor and REST API, register them:

The sanitize_callback cleans input before it’s saved, and auth_callback controls who can edit the value. Skipping these is a security shortcut you’ll regret.

Using a field plugin

For most projects, I use a field framework such as Advanced Custom Fields or a similar tool to build the editing interface. Writing meta boxes by hand is possible, but a good field plugin saves days of work and gives editors a clean, friendly screen. Repeaters, image fields, date pickers and relationship fields come built in.

The important thing is to plan your fields before building. Sit down with the content and list every piece of information each item needs, what type it is, whether it’s required, and how it’ll be displayed. Ten minutes of planning saves a messy migration later.

Displaying the content

Once the structure exists, you need templates. In a classic theme, WordPress looks for files like single-case_study.php and archive-case_study.php. In a block theme, you create templates in the Site Editor or as HTML template files.

To query custom content anywhere, use WP_Query:

Note the esc_html calls. Always escape output, even for your own fields.

Performance considerations

Custom fields are flexible, but the postmeta table isn’t built for heavy filtering. Querying thousands of posts by several meta values at once can get slow, because meta values aren’t indexed the way normal columns are.

A few guidelines:

  • Use taxonomies for anything you filter or group by. Taxonomy queries are much faster than meta queries.
  • Avoid ordering large result sets by meta values on every page load. Cache the results if you must.
  • For very large datasets with complex filtering, consider a custom table or a search service.

Structured content pays off in SEO

Once content is structured, adding schema markup becomes straightforward. A recipe post type with fields for cook time and ingredients maps directly to Recipe schema. Events map to Event schema, products to Product schema. Search engines and AI answer engines understand structured data much better than a paragraph with bold text.

Common mistakes to avoid

  • Registering post types in the theme instead of a plugin.
  • Forgetting show_in_rest and losing block editor support.
  • Using a reserved name such as post, page, action or order as the post type key. Keep keys lowercase, under twenty characters and specific.
  • Creating a post type for something that only has one or two entries. That’s a page.
  • Storing filterable data in custom fields when a taxonomy would perform better.

Planning a content model before you write code

Before registering anything, I sketch the content model on paper or in a simple spreadsheet. It’s the step most people skip, and it’s the one that saves the most time later.

For each type of content, write down three things. First, the fields: every piece of information an item needs, with its type, such as text, number, date, image or link. Second, the relationships: does a case study link to a service, does an event belong to a venue, does a team member appear on several projects? Third, how the content will be used: which pages list it, how visitors filter it and what appears in search results.

Here’s a quick example for a training company. Courses get their own post type, with fields for duration, price, level and next start date. Trainers are a separate post type, because one trainer teaches several courses and needs their own profile page. Course categories become a taxonomy, because visitors browse by subject. Locations might be another taxonomy if courses run in several cities.

Once it’s written down, the code almost writes itself, and you’ll spot problems early. If you realise that “level” is something visitors filter by, you’ll make it a taxonomy instead of a field before any content is entered, rather than migrating hundreds of items later.

It’s also worth showing the plan to the people who’ll edit the content. They often point out fields you missed, like a “featured” flag for the homepage or a short summary for listing cards. A twenty-minute conversation at this stage is far cheaper than a rebuild after launch.

Frequently asked questions

Do custom post types affect SEO?

They can help. Custom post types give you clean URLs, dedicated archives and the ability to add accurate schema markup. Just make sure the new archives are useful pages, include them in your sitemap and avoid creating thin, empty archives.

Should I use a plugin to create post types?

Plugins that register post types through a settings screen are fine for simple sites. For anything a developer maintains, I prefer code in a custom plugin. It’s version controlled, easy to review and doesn’t depend on another plugin staying active.

Can I convert existing posts into a custom post type?

Yes. Plugins exist to change post types in bulk, or you can do it with WP-CLI or a small script. Remember to add redirects from the old URLs to the new ones.

What happens to my data if I remove the code?

The content stays in the database, but WordPress hides it until the post type is registered again. Nothing is deleted. That’s another reason to keep registration in a plugin rather than a theme.

Wrapping up

Custom post types, taxonomies and fields turn WordPress from a blogging tool into a real content management system. The setup takes a little planning, but the payoff is huge: editors get clear forms instead of formatting rules, developers get clean data to work with, and the site gains filtering, better search and stronger SEO. If your content has structure, give it a structure in WordPress too.