Skip to main content

Custom Post Types / Taxonomies

When a site needs more than the news, add a custom post type. Define one post type per file in settings/post-types/; TOBIUO registers it. The case studies (settings/post-types/case.php) are the sample.

// settings/post-types/case.php
return [
'post_type' => 'case',
'args' => [
'label' => '事例紹介',
'public' => true,
'has_archive' => true,
'rewrite' => ['slug' => 'case', 'with_front' => false],
'supports' => ['title', 'editor', 'thumbnail', 'excerpt', 'revisions'],
'show_in_rest' => true,
],
'taxonomies' => [
'case_category' => [
'label' => '事例カテゴリー',
'hierarchical' => true,
'rewrite' => ['slug' => 'case/category', 'with_front' => false],
'seo' => ['description' => '事例紹介のカテゴリー別の一覧です。'],
],
],
'permalink' => ['structure' => '/%postname%/'],
'seo' => ['title' => '事例紹介', 'description' => '事例紹介の一覧です。'],
];
  • args is passed to register_post_type().
  • taxonomies maps each taxonomy name to the arguments for register_taxonomy().
    • Its seo applies to every term of the taxonomy and is not passed to register_taxonomy(). Leave out title; each term's name is its title.
  • permalink is the post URL structure. Without it, the post type keeps WordPress's default URLs (/case/test-case-1/).
  • seo holds the archive's SEO settings. See SEO.
  • Keep 'with_front' => false in every rewrite; otherwise /news/ is put in front of the URLs.

After adding or changing a definition, open Settings → Permalinks once to regenerate the rewrite rules. Tools → Content Structure (TOBIUO) shows a warning while rules are missing.

// /case/test-case-1/
'permalink' => ['structure' => '/%postname%/'],

// /case/sample/test-case-1/ (includes the category)
'permalink' => ['structure' => '/%case_category%/%postname%/'],

// /case/2026/10/test-case-1/ (includes the date, with date archives at /case/2026/10/)
'permalink' => ['structure' => '/%year%/%monthnum%/%postname%/', 'dateArchive' => true],
  • structure must contain %postname% or %post_id%. It also accepts the date tags, %author% and the taxonomies of the same post type (%case_category%).
  • dateArchive and authorArchive add /case/2026/10/ and /case/author/name/. Link to them with tobiuo_get_year_link(), tobiuo_get_month_link(), tobiuo_get_day_link() and tobiuo_get_author_link().
  • A URL with the wrong category (/case/other/test-case-1/) is redirected to the right one with a 301.

See the TOBIUO documentation for the details.

Category URLs​

The usual shape puts the post type slug in front of the taxonomy slug.

// /case/category/sample/
'rewrite' => ['slug' => 'case/category', 'with_front' => false],

// /case-category/sample/ (without the post type slug)
'rewrite' => ['slug' => 'case-category', 'with_front' => false],
warning

Don't create a post or term whose slug is the last part of the taxonomy slug (category in this example). Their URLs would overlap.

Templates​

PageTemplate
Archivearchive-{post type}.php
Postsingle-{post type}.php
Taxonomy archivetaxonomy-{taxonomy}.php

When the post type archive and the taxonomy archive share a layout, load one from the other.

// taxonomy-case_category.php
<?php
get_template_part('archive', 'case');
// archive-case.php
<?php
get_header();

// The term being shown, or null on the post type archive
$current_term = is_tax('case_category') ? get_queried_object() : null;

while (have_posts()) : the_post();
// Every case study, or only those in $current_term
endwhile;