1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WordPressで質問投稿&管理画面から回答できるFAQシステムを自作してみた

Posted at

— カスタム投稿 + カスタム分類 + 質問/回答フィールドで実装する ―


✅ 概要

ユーザーが質問を投稿し、管理画面で回答を書いて公開できる FAQ システムを プラグイン不要 で構築します。

実装ポイント 説明
カスタム投稿タイプ faq_question
カスタム分類 faq_category
メタボックス 質問テキスト/回答テキスト
タイトル自動生成 タイトル未入力時は質問文から自動生成

🌐 動作環境

  • WordPress 6.x 以上(推奨)
  • テーマの functions.php に追記、または 独自プラグイン化

🧱 カスタム投稿タイプの登録

function faq_register_post_type() {
  register_post_type( 'faq_question', array(
    'labels' => array(
      'name'               => 'FAQ質問',
      'singular_name'      => 'FAQ質問',
      'add_new_item'       => '新規質問を追加',
      'edit_item'          => '質問を編集',
      'view_item'          => '質問を見る',
      'search_items'       => '質問を検索',
      'not_found'          => '質問が見つかりませんでした',
    ),
    'public'          => true,
    'has_archive'     => true,
    'menu_position'   => 5,
    'menu_icon'       => 'dashicons-editor-help',
    'supports'        => array( 'title' ),   // 本文エディタは不要
  ) );
}
add_action( 'init', 'faq_register_post_type' );


🏷️ カスタム分類(カテゴリ)の登録

function faq_register_taxonomy() {
  register_taxonomy( 'faq_category', 'faq_question', array(
    'labels' => array(
      'name'              => 'FAQカテゴリ',
      'singular_name'     => 'FAQカテゴリ',
      'search_items'      => 'カテゴリを検索',
      'all_items'         => 'すべてのカテゴリ',
      'edit_item'         => 'カテゴリを編集',
      'update_item'       => 'カテゴリを更新',
      'add_new_item'      => '新しいカテゴリを追加',
      'new_item_name'     => '新しいカテゴリ名',
      'menu_name'         => 'FAQカテゴリ',
    ),
    'hierarchical'      => true,   // 親子関係を持たせる
    'show_ui'           => true,
    'show_admin_column' => true,
    'public'            => true,
  ) );
}
add_action( 'init', 'faq_register_taxonomy' );

📝 質問・回答入力用メタボックス追加

/* メタボックス追加 */
function faq_add_meta_boxes() {
  add_meta_box(
    'faq_question_box',
    '質問・回答の入力',
    'faq_render_meta_box',
    'faq_question',
    'normal',
    'high'
  );
}
add_action( 'add_meta_boxes', 'faq_add_meta_boxes' );

/* 入力エリア描画 */
function faq_render_meta_box( $post ) {
  $question = get_post_meta( $post->ID, 'faq_question_text', true );
  $answer   = get_post_meta( $post->ID, 'faq_answer', true );
  ?>
  <p><label><strong>質問内容</strong></label></p>
  <textarea name="faq_question_text" style="width:100%;" rows="3"><?php echo esc_textarea( $question ); ?></textarea>

  <p><label><strong>回答</strong></label></p>
  <textarea name="faq_answer" style="width:100%;" rows="6"><?php echo esc_textarea( $answer ); ?></textarea>
  <?php
}

💾 メタデータ保存 & タイトル自動生成

function faq_save_meta_boxes( $post_id ) {

  /* === ガード節 === */
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  if ( wp_is_post_revision( $post_id ) )               return;
  if ( get_post_type( $post_id ) !== 'faq_question' )  return;
  if ( ! current_user_can( 'edit_post', $post_id ) )   return;

  /* 質問保存 */
  if ( isset( $_POST['faq_question_text'] ) ) {
    update_post_meta(
      $post_id,
      'faq_question_text',
      sanitize_textarea_field( $_POST['faq_question_text'] )
    );
  }

  /* 回答保存 */
  if ( isset( $_POST['faq_answer'] ) ) {
    update_post_meta(
      $post_id,
      'faq_answer',
      sanitize_textarea_field( $_POST['faq_answer'] )
    );
  }

  /* タイトル未入力 → 自動生成 */
  $post_obj = get_post( $post_id );
  if ( empty( $post_obj->post_title ) ) {
    $question = get_post_meta( $post_id, 'faq_question_text', true );
    $auto     = wp_trim_words( $question, 10, '…' ) ?: '質問_' . current_time( 'Ymd_His' );

    remove_action( 'save_post', 'faq_save_meta_boxes' );      // ループ防止
    wp_update_post( array(
      'ID'         => $post_id,
      'post_title' => $auto,
    ) );
    add_action( 'save_post', 'faq_save_meta_boxes' );
  }
}
add_action( 'save_post', 'faq_save_meta_boxes' );

🔍 表示用テンプレート(例)

$question   = get_post_meta( get_the_ID(), 'faq_question_text', true );
$answer     = get_post_meta( get_the_ID(), 'faq_answer', true );
$categories = get_the_terms( get_the_ID(), 'faq_category' );

あとは HTML に流し込んでデザインすれば OK です。
「質問が未回答なら非表示にする」「カテゴリ別ページを作る」といった条件分岐も簡単に行えます。


🏁 まとめ

1. カスタム投稿タイプ で質問を管理
2. カスタム分類 でカテゴリ分け
3. メタボックス で質問・回答を入力
4. タイトルは 自動生成 で運用ラクラク

プラグインに頼らず、自分のサイト要件に合わせた軽量な FAQ 機能が完成します。


🛠 さらなる拡張アイデア

  • 投稿フォーム(wp_insert_post() + Nonce)でユーザーから質問を直接受付
  • REST API + JavaScript で検索&ページ遷移なしの動的FAQ
  • 質問ステータス(回答待ち / 公開済み)の管理カラム追加

これらを加えれば運用の幅がグッと広がるはず!


今回紹介したWordPressのカスタム投稿タイプとメタボックスを使ったFAQシステムの作り方は、基本の骨組みですが、カスタマイズ次第で無限に広がります。質問投稿フォームの設置や回答の管理、カテゴリ分けなど、自分のサイトに合わせて柔軟に改良してみてください。

基本的に、自分用の備忘録ですが、
もしこの記事が役立ったら、ぜひ「いいね」やコメントで教えてもらえると嬉しいです。
みなさんのサイト作りがもっと楽しく、便利になるお手伝いができれば幸いです!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?