【WordPress】記事の情報を取得する方法|ID・タイトル・本文・カテゴリーの取得関数とWP_Query

【WordPress】ID,タイトル,本文など記事の情報を取得する方法 WordPress

WordPressのテーマ開発では、記事のID・タイトル・本文・抜粋・カテゴリー・タグなどの情報をコードで取得する場面が頻繁にあります。この記事では、取得方法を①ループ内の取得関数 ②ループ外で取得する方法 ③WP_Queryで条件付き取得の3つに整理して解説します。

この記事でわかること

  • ループ内でよく使う取得関数の一覧(ID・タイトル・本文ほか)
  • get_the_content()the_content()違いと注意点
  • ループ外で get_post() / get_post_field() から取得する
  • WP_Query で条件に合う記事を取得する
スポンサーリンク

ループ内でよく使う取得関数

投稿ループ(have_posts() / the_post() の中)では、次の関数で現在の記事の情報を取得できます。the_◯◯()その場で出力(echo)get_the_◯◯()値を返す(変数に入れて加工できる)のが基本的な違いです。

取得したい情報 関数 戻り値
記事ID get_the_ID() 投稿ID(数値)
タイトル get_the_title() / the_title() タイトル文字列
本文 get_the_content() / the_content() 本文(※後述の注意あり)
抜粋 get_the_excerpt() 抜粋文字列
パーマリンク get_permalink() 記事URL
投稿日 get_the_date('Y-m-d') 日付文字列
著者名 get_the_author() 著者名
カテゴリー get_the_category() カテゴリーの配列
タグ get_the_tags() タグの配列
アイキャッチID get_post_thumbnail_id() 画像の添付ID

get_the_content() と the_content() の違いに注意

本文の取得でつまずきやすいのが get_the_content() です。この関数はショートコードやブロック、自動整形(wpautop)が適用されていない生の本文を返します。そのまま echo すると、ショートコードが文字のまま表示されたり、ブロックが正しく描画されなかったりします。

NG:ショートコード・ブロックが展開されない
echo get_the_content(); // 生テキスト。[shortcode] がそのまま出る

整形済みの本文を表示したいときは、その場で出力する the_content() を使うか、get_the_content()the_content フィルターを通します。

OK:フィルターを適用して整形済みの本文を出す
the_content(); // 出力するだけならこれが簡単(フィルター適用済み)

// 値として受け取りたい場合
$html = apply_filters('the_content', get_the_content());
echo $html;

ループ外で取得する(get_post / get_post_field)

投稿ループの外で特定の記事の情報を取得したい場合は、get_post($id)WP_Post オブジェクトを取得してプロパティにアクセスするか、get_post_field() で項目を直接取得します。

ループ外で記事情報を取得する
$post_id = 123;

// WP_Post オブジェクトから
$post = get_post($post_id);
echo esc_html($post->post_title); // タイトル

// 項目を直接取得
$title = get_post_field('post_title', $post_id);
$slug  = get_post_field('post_name', $post_id);

// 本文は the_content フィルターを通して整形
echo apply_filters('the_content', get_post_field('post_content', $post_id));

WP_Query で条件に合う記事を取得する

「カテゴリーIDが5の記事だけ」のように条件を指定して複数記事を取得するには WP_Query を使います。ループの最後に wp_reset_postdata() を必ず呼びます。

WP_Query:条件に合う記事のタイトルと本文を出力
$query = new WP_Query(array('cat' => 5));

if ($query->have_posts()) {
  while ($query->have_posts()) {
    $query->the_post();
    the_title('<h2>', '</h2>'); // 出力
    the_content();              // フィルター適用済みの本文を出力
  }
  wp_reset_postdata(); // ループ後に必ずリセット
}

カテゴリー・タグを取得する

カテゴリーは get_the_category()、タグは get_the_tags()配列として取得し、foreach で名前などを取り出します。出力時は esc_html() でエスケープしましょう。

カテゴリー名・タグ名を出力する
// カテゴリー
$categories = get_the_category();
if (!empty($categories)) {
  foreach ($categories as $category) {
    echo esc_html($category->name);
  }
}

// タグ
$tags = get_the_tags();
if (!empty($tags)) {
  foreach ($tags as $tag) {
    echo esc_html($tag->name);
  }
}

カテゴリー取得のより詳しい方法(リンク付き出力・カスタムタクソノミー対応など)は記事のカテゴリーを取得する方法(完全ガイド)を参照してください。

よくある質問(FAQ)

Qget_the_content() で本文を出すとショートコードが文字のまま表示されます。
Aget_the_content() はフィルター未適用の生テキストを返すためです。表示するだけなら the_content() を使うか、echo apply_filters('the_content', get_the_content()); のようにthe_content フィルターを通すとショートコード・ブロックが展開されます。
Qループの外で特定記事の情報を取得するには?
Aget_post($id)WP_Post を取得し $post->post_title のようにアクセスするか、get_post_field('post_title', $id) で項目を直接取得します。本文を整形して出す場合は the_content フィルターを通してください。
Qカスタムフィールドや更新日時を取得するには?
Aカスタムフィールドは get_post_meta($post_id, $meta_key, true)(第3引数 true で単一値)。ACFを使っているなら get_field("フィールド名") が便利です。更新日時は get_the_modified_date("Y-m-d")、公開日は get_the_date() で取得できます。

まとめ

  • ループ内get_the_ID() / get_the_title() など。出力だけなら the_◯◯()
  • 本文の注意get_the_content() は生テキスト。表示は the_content() かフィルター適用
  • ループ外get_post() / get_post_field()
  • 条件付きWP_Querywp_reset_postdata()

用途に応じて取得方法を使い分け、出力時は esc_html() などでエスケープすることを忘れないようにしましょう。