2026年5月20日
2026年5月20日
WordPressプラグインでカスタム投稿タイプを登録する方法
はじめに
WordPressのカスタム投稿タイプ(CPT: Custom Post Type)は register_post_type() 関数で作成します。ポートフォリオ・求人・物件・レシピなど標準の「投稿」と別に管理する独自コンテンツタイプを追加できます。
症状・原因
- WordPressに「物件」「レシピ」など独自のコンテンツタイプを追加したい
- カスタム投稿タイプのREST APIサポートを有効にしたい
- カスタム分類(タクソノミー)をカスタム投稿タイプに紐付けたい
- WP-CLIでカスタム投稿タイプの投稿を管理したい
解決手順
ステップ1:カスタム投稿タイプを登録する
// プラグインファイルまたは functions.php
add_action('init', function(): void {
register_post_type('property', [
'labels' => [
'name' => '物件',
'singular_name' => '物件',
'add_new' => '物件を追加',
'add_new_item' => '新規物件を追加',
'edit_item' => '物件を編集',
'new_item' => '新しい物件',
'view_item' => '物件を表示',
'search_items' => '物件を検索',
'not_found' => '物件が見つかりません',
'not_found_in_trash' => 'ゴミ箱に物件はありません',
],
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true, // ブロックエディター・REST API対応
'query_var' => true,
'rewrite' => ['slug' => 'property'],
'capability_type' => 'post',
'has_archive' => true, // アーカイブページを有効化
'hierarchical' => false, // 階層構造 false=投稿型 true=ページ型
'menu_position' => 5,
'menu_icon' => 'dashicons-building',
'supports' => [
'title',
'editor',
'thumbnail',
'excerpt',
'custom-fields',
'revisions',
],
]);
});
ステップ2:カスタム分類(タクソノミー)を登録する
add_action('init', function(): void {
// 階層あり(カテゴリーのような分類)
register_taxonomy('property_type', ['property'], [
'labels' => [
'name' => '物件タイプ',
'singular_name' => '物件タイプ',
'add_new_item' => '新規物件タイプを追加',
'edit_item' => '物件タイプを編集',
],
'hierarchical' => true,
'show_ui' => true,
'show_in_rest' => true,
'rewrite' => ['slug' => 'property-type'],
]);
// 階層なし(タグのような分類)
register_taxonomy('property_feature', ['property'], [
'labels' => [
'name' => '設備・特徴',
'singular_name' => '設備・特徴',
],
'hierarchical' => false,
'show_ui' => true,
'show_in_rest' => true,
'rewrite' => ['slug' => 'property-feature'],
]);
});
ステップ3:パーマリンクをリセットする
# CPT登録後は必ずパーマリンクをリセット
wp rewrite flush --hard
# 登録されたCPTを確認
wp post-type list --format=table
# CPTの詳細情報を確認
wp post-type get property --format=json
# 登録されたタクソノミーを確認
wp taxonomy list --format=table
ステップ4:カスタム投稿タイプの投稿を管理する
# カスタム投稿タイプの投稿一覧
wp post list --post_type=property --format=table
# 新しい物件を追加
wp post create \
--post_type=property \
--post_title="東京都港区の1LDK" \
--post_status=publish
# タームを投稿に設定
wp post term set {POST_ID} property_type "マンション"
wp post term set {POST_ID} property_feature "オートロック" "宅配ボックス"
# 特定タームの投稿数を確認
wp term list property_type --format=table
# カスタムフィールドを設定
wp post meta update {POST_ID} _property_rent 150000
wp post meta update {POST_ID} _property_area 45.5
ステップ5:REST APIでカスタム投稿タイプを使う
# REST APIで物件一覧を取得(show_in_rest=trueが必要)
curl https://example.com/wp-json/wp/v2/property
# 特定の物件を取得
curl https://example.com/wp-json/wp/v2/property/{ID}
# フィルタリング(物件タイプで絞り込み)
curl "https://example.com/wp-json/wp/v2/property?property_type=マンション"
ステップ6:カスタム投稿タイプをWP_Queryで取得する
// テンプレートファイルやウィジェット内で使用
$properties = new WP_Query([
'post_type' => 'property',
'posts_per_page' => 10,
'post_status' => 'publish',
'tax_query' => [
[
'taxonomy' => 'property_type',
'field' => 'slug',
'terms' => 'mansion',
],
],
'meta_query' => [
[
'key' => '_property_rent',
'value' => 200000,
'compare' => '<=',
'type' => 'NUMERIC',
],
],
'orderby' => 'date',
'order' => 'DESC',
]);
if ($properties->have_posts()) {
while ($properties->have_posts()) {
$properties->the_post();
echo '<h2>' . esc_html(get_the_title()) . '</h2>';
}
wp_reset_postdata();
}
注意事項
- CPTを登録・変更した後は必ず
wp rewrite flush --hardでパーマリンクをリセットしてください show_in_rest => trueを設定しないとGutenbergエディターが使えません- CPTのスラッグは一度公開後は変更しないでください(既存URLが404になります)
まとめ
カスタム投稿タイプは init フックで register_post_type() を呼ぶだけで追加できます。show_in_rest => true でブロックエディターとREST APIに対応し、register_taxonomy() でカスタム分類も紐付けられます。登録後は wp rewrite flush を忘れずに実行してください。