2026年11月8日

2026年11月8日

WordPressのカスタムタクソノミーが表示されない問題を解決する方法

はじめに

カスタムタクソノミーを登録してもフロントエンドに表示されない・管理画面のメニューに出ない・タクソノミーアーカイブが404になる問題は、register_taxonomy()の設定ミスやパーマリンクのキャッシュが原因です。

症状・原因

  • 管理画面の投稿編集画面にタクソノミーのメタボックスが表示されない
  • タクソノミーのアーカイブページが404になる
  • ナビゲーションメニューにタクソノミーの項目が出ない
  • フロントエンドでタクソノミーのタームが表示されない

解決手順

ステップ1:タクソノミーの登録状態を確認する

# 登録済みタクソノミーを確認
wp eval "
\$taxonomies = get_taxonomies([], 'objects');
foreach (\$taxonomies as \$tax) {
    if (\$tax->_builtin) continue;
    printf(
        '%s: public=%s, show_ui=%s, object_types=%s' . PHP_EOL,
        \$tax->name,
        var_export(\$tax->public, true),
        var_export(\$tax->show_ui, true),
        implode(',', \$tax->object_type)
    );
}
"

# 特定のタクソノミーのターム一覧を確認
wp term list product_cat --fields=term_id,name,slug,count

ステップ2:register_taxonomy を正しく設定する

// functions.php: カスタムタクソノミーを正しく登録
add_action('init', function(): void {
    // ① タクソノミーを投稿タイプより先に登録
    register_taxonomy('product_cat', ['product'], [
        'label'             => '商品カテゴリー',
        'labels'            => [
            'name'          => '商品カテゴリー',
            'singular_name' => '商品カテゴリー',
            'all_items'     => 'すべてのカテゴリー',
            'edit_item'     => 'カテゴリーを編集',
            'add_new_item'  => '新しいカテゴリーを追加',
        ],
        'public'            => true,        // フロントエンドで公開
        'show_ui'           => true,        // 管理画面に表示
        'show_in_nav_menus' => true,        // ナビゲーションメニューに表示
        'show_admin_column' => true,        // 投稿一覧にカラム表示
        'hierarchical'      => true,        // カテゴリー型(false=タグ型)
        'show_in_rest'      => true,        // Gutenberg対応
        'rewrite'           => [
            'slug'         => 'product-category',
            'with_front'   => false,
            'hierarchical' => true,
        ],
    ]);
});

// ② カスタム投稿タイプに後からタクソノミーを紐付ける場合
add_action('init', function(): void {
    register_taxonomy_for_object_type('product_cat', 'product');
}, 11); // register_taxonomy より後に実行
# 登録後はパーマリンクをリセット
wp rewrite flush --hard

# タクソノミーアーカイブのURLを確認
wp eval "
\$term = get_term_by('slug', 'accessories', 'product_cat');
echo get_term_link(\$term) . PHP_EOL;
"

ステップ3:タクソノミーアーカイブのテンプレートを作成する

// テーマファイル: taxonomy-product_cat.php
// WordPressテンプレート階層: taxonomy-{taxonomy}-{term}.php > taxonomy-{taxonomy}.php > taxonomy.php

get_header();
?>
<main>
    <header>
        <h1><?php single_term_title(); ?></h1>
        <?php if (term_description()): ?>
            <div class="taxonomy-description">
                <?php echo wp_kses_post(term_description()); ?>
            </div>
        <?php endif; ?>
    </header>

    <?php if (have_posts()): ?>
        <div class="product-grid">
            <?php while (have_posts()): the_post(); ?>
                <article <?php post_class(); ?>>
                    <?php the_post_thumbnail('medium'); ?>
                    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                </article>
            <?php endwhile; ?>
        </div>
        <?php the_posts_pagination(); ?>
    <?php else: ?>
        <p>この分類には商品がありません。</p>
    <?php endif; ?>
</main>
<?php get_footer();

ステップ4:管理画面でタクソノミーを表示する

// functions.php: 管理画面のカラムにタクソノミーを追加
// ※ show_admin_column=true で自動追加されるが、手動で制御する場合

add_filter('manage_product_posts_columns', function(array $columns): array {
    // 既存のカラムの後にタクソノミーカラムを追加
    $columns['product_cat'] = '商品カテゴリー';
    return $columns;
});

add_action('manage_product_posts_custom_column', function(string $column, int $post_id): void {
    if ($column === 'product_cat') {
        $terms = get_the_terms($post_id, 'product_cat');
        if ($terms && !is_wp_error($terms)) {
            $names = wp_list_pluck($terms, 'name');
            echo esc_html(implode(', ', $names));
        } else {
            echo '—';
        }
    }
}, 10, 2);

// タクソノミーのターム選択をラジオボタンに変更(単一選択の場合)
add_action('admin_footer', function(): void {
    $screen = get_current_screen();
    if ($screen && $screen->post_type === 'product') {
        ?>
        <script>
        // チェックボックスをラジオボタンに変換
        document.querySelectorAll('#product_cat-checklist input[type="checkbox"]')
            .forEach(cb => { cb.type = 'radio'; });
        </script>
        <?php
    }
});

ステップ5:フロントエンドでタームを取得・表示する

// functions.php / テンプレートでのターム取得
// 投稿に紐付いたタームを表示
function display_product_categories(int $post_id): void {
    $terms = get_the_terms($post_id, 'product_cat');
    if (empty($terms) || is_wp_error($terms)) {
        return;
    }

    $links = array_map(function(WP_Term $term): string {
        return sprintf(
            '<a href="%s">%s</a>',
            esc_url(get_term_link($term)),
            esc_html($term->name)
        );
    }, $terms);

    echo '<div class="product-cats">' . implode(', ', $links) . '</div>';
}

// ウィジェット代わりにタクソノミー一覧を表示
function get_product_cat_list(): string {
    $terms = get_terms([
        'taxonomy'   => 'product_cat',
        'hide_empty' => false,
        'orderby'    => 'name',
        'order'      => 'ASC',
    ]);

    if (empty($terms) || is_wp_error($terms)) {
        return '';
    }

    $items = array_map(function(WP_Term $term): string {
        return sprintf(
            '<li><a href="%s">%s (%d)</a></li>',
            esc_url(get_term_link($term)),
            esc_html($term->name),
            $term->count
        );
    }, $terms);

    return '<ul class="product-cat-list">' . implode('', $items) . '</ul>';
}

注意事項

  • register_taxonomy()とregister_post_type()は同じinitフックで登録しますが、タクソノミーは投稿タイプより先に登録するか、register_taxonomy_for_object_type()で後から紐付けてください
  • public=trueにするとフロントエンドのアーカイブが有効になります。アーカイブが不要な場合はpublicly_queryable=falseを追加してください
  • タクソノミーのスラッグと同名のページが存在すると競合します。パーマリンクリセット後も404が続く場合はスラッグの競合を確認してください

まとめ

カスタムタクソノミーの表示問題は①wp evalで登録状態(public・show_ui・object_types)を確認、②register_taxonomy()でpublic=true・show_ui=true・show_in_rest=true・rewriteを正しく設定してinitフックで登録、③wp rewrite flush --hardでパーマリンクをリセット、④テーマにtaxonomy-product_cat.phpを作成してアーカイブテンプレートを実装、⑤get_the_terms()とget_term_link()でフロントエンドにターム一覧を表示します。

お気軽にご相談ください

お見積りへ お問い合わせへ
▲