Blog / Build with AI /
Custom post types with AI help
Standard WordPress gives you posts and pages. That’s enough for a blog. It isn’t enough when you need to store something else: recipes, events, team members, products, case studies. If everything goes in as a “post”, your admin gets messy fast, and so does your site’s structure.
That’s the job of a custom post type: a new kind of content with its own menu, fields and URLs. An AI chatbot can draft the code for one quickly. The question is how to do it without breaking your site.
What a custom post type is
Think of WordPress as a filing cabinet. Posts and pages are two drawers. A custom post type is a new, labelled drawer for a specific kind of thing. “Events” go in the Events drawer, with their own list in the dashboard, their own address pattern like /events/launch-party/ and their own settings.
You can also add taxonomies (custom categories, like “Event type”) and custom fields (extra data, like date or venue) to go with them.
When you need one
- You publish a repeatable type of content with its own structure.
- You want it kept apart from blog posts, in menus, archives and feeds.
- You want different templates for it.
- You want a tidy admin for editors.
When you don’t: if a normal category and a tag will do, don’t add complexity. A custom post type is another thing to maintain.
Where to put the code, safely
This is the most important decision, and the one that causes the most grief.
- Not in your theme’s
functions.phpif you can avoid it. If you change theme, your content type vanishes from the admin, though the content stays in the database. The pages then become hard to reach. - In a small plugin of your own. A single PHP file in
wp-content/plugins/is enough. It keeps the content type alive whatever the theme. - Or in a code-snippets plugin, if you prefer not to touch files.
I covered the thinking behind safe edits in automating boring WordPress tasks with AI-written snippets. The same rules apply: backup first, test on a copy, change one thing at a time.
Step 1: describe what you need
A good prompt names everything:
Write the PHP for a small WordPress plugin that registers a custom post type called “Event” (slug
event, URL/events/). It should support title, editor, thumbnail and excerpt. Show in the admin menu with a calendar dashicon. Include an archive page. Show in the REST API. Use a text domainmy-events. Add the plugin header comment. Follow WordPress coding standards, and useregister_post_typeon theinithook.
Then ask for a custom taxonomy in a second prompt, and extra fields in a third. One change at a time.
Step 2: read what it gave you
You should see roughly: a plugin header comment, a function hooked into init, and a call to register_post_type with an array of labels and arguments. Check:
- The slug is lowercase, at most 20 characters, and not a name WordPress reserves, such as
post,pageorattachment. - It uses
init, not something odd. - No direct database queries you didn’t ask for.
- No code that sends data elsewhere.
- Function names have a unique prefix, so they can’t clash with other plugins.
Step 3: test on a copy
Install it on a staging or local copy of your site, not the live one. Then:
- Activate the plugin. Do you see the new menu item?
- Add a test event and publish it.
- Visit the front-end URL. If you get a “not found” page, go to Settings, then Permalinks, and press Save. This refreshes the rewrite rules and fixes the problem most of the time.
- Check the archive page at
/events/. - Deactivate and reactivate to make sure nothing errors.
Turn on WordPress debug logging on the test site so you can see PHP warnings.
Step 4: add custom fields, carefully
If you want a date and venue, you can use a plugin built for custom fields, or ask the AI to write meta boxes. The second involves saving user input, where AI-written code often forgets:
- A nonce check, to confirm the request is genuine
- A capability check, so only users with permission can save
- Sanitising values before saving, and escaping them on output
Ask directly: “Does this code verify a nonce, check capabilities, sanitise input and escape output?” If any answer is no, ask for the fix. These are the standard security habits of WordPress development, and they’re the ones a chatbot skips.
Step 5: think about SEO and structure
New content types affect how your site is crawled. Decide:
- Should the archive and single pages be indexable? Usually yes for real content.
- What are the URL patterns, and will they stay stable? Changing them later needs redirects.
- Do they appear in your XML sitemap? Many SEO plugins include public post types automatically; check yours.
- Do they have titles and descriptions that make sense?
What can go wrong
- 404s after activation: flush permalinks.
- Content type disappears after a theme change: it lived in the theme.
- Clashing slug: it collides with an existing page.
- Editor shows the old editor or blocks don’t work: check you’ve turned on
show_in_rest. - Messy output on the front-end: you need a template file, such as
single-event.php, in your theme or child theme.
Key takeaways
- Use a custom post type for a distinct kind of repeatable content.
- Put the code in a plugin, not in the theme.
- Test on a copy, save permalinks, and check security habits in any meta box code.
- Plan URLs and indexing before you publish a hundred items.
Related reading
What would you put in your first custom post type: events, case studies, or something else?