
WordPressのグローバル変数 $post には投稿に関するオブジェクトです。投稿のID・タイトル・本文やその他の投稿にまつわる情報が、この $post に格納されています。
どんな情報が入っているか、 $post から特定の情報を引き出すためのパラメーターを知っておくとWordPressのサイト制作もスムーズです。ただ全ての情報を覚えておくのは大変なので、情報を引き出したいときに使うためのメモです。
‘$post’ の中身を確認
投稿ページで var_dump($post) 記載すると、その投稿の情報を確認することができます。
上記のコードを書いて保存すると、そのページに以下のような情報がずらりと並びます。(以下の値は私のローカル環境のものです)
object(WP_Post)#5904 (24) {
["ID"]=> int(295)
["post_author"]=> string(1) "1"
["post_date"]=> string(19) "2023-07-17 20:42:52"
["post_date_gmt"]=> string(19) "2023-07-17 11:42:52"
["post_content"]=> string(0) ""
["post_title"]=> string(39) "サンプル"
["post_excerpt"]=> string(0) ""
["post_status"]=> string(7) "publish"
["comment_status"]=> string(4) "open"
["ping_status"]=> string(4) "open"
["post_password"]=> string(0) ""
["post_name"]=> string(7) "sample"
["to_ping"]=> string(0) ""
["pinged"]=> string(0) ""
["post_modified"]=> string(19) "2023-07-17 20:43:05"
["post_modified_gmt"]=> string(19) "2023-07-17 11:43:05"
["post_content_filtered"]=> string(0) ""
["post_parent"]=> int(0)
["guid"]=> string(31) "https://local.local/?p=295"
["menu_order"]=> int(0)
["post_type"]=> string(4) "post"
["post_mime_type"]=> string(0) ""
["comment_count"]=> string(1) "0"
["filter"]=> string(3) "raw"
}
それぞれどんな情報なのか以下にまとめます。
| プロパティ | 内容 |
|---|---|
| ID | 記事のID |
| post_author | 記事の作成者(ユーザー) |
| post_date | 記事の作成日 |
| post_date_gmt | 記事の作成日(グリニッジ標準) グリニッジ標準とは |
| post_content | 記事の本文 |
| post_title | 記事のタイトル |
| post_excerpt | 記事の抜粋 |
| post_status | 記事の状態(公開・非公開・下書きなど) |
| comment_status | コメントの状態(公開・非公開) |
| ping_status | ピンバックの受付状態(可否) |
| post_password | 記事のパスワード(パスワード保護されている場合) |
| post_name | 記事のスラッグ名(パーマリンク設定で%postname%を使用している場合) |
| to_ping | ping送信先 |
| pinged | トラックバック送信先 |
| post_modified | 記事の更新日 |
| post_modified_gmt | 記事の更新日(グリニッジ標準) |
| post_content_filtered | フィルター処理の後の記事本文 |
| post_parent | 記事の親のID |
| guid | デフォルトのURL(URLをカスタマイズしていない状態) |
| menu_order | 表示順序 |
| post_type | 投稿タイプ(投稿・固定ページ・カスタム投稿など) |
| post_mime_type | 添付ファイルのMIME形式 |
| comment_count | コメント数 |
| filter | サニタイズ形式 |
‘$post’ のパラメーターで特定の情報を取得
$post->プロパティ名 といった形で $post にある一部の情報を引き出すことができます。
タイトルなら $post->post_title 、本文は $post->post_content です。グローバル宣言をすることも忘れずに。
PHP
// グローバル宣言
global $post;
// $postから特定の情報を取得
$post->post_title
$post->post_contentカテゴリー : WordPress
タグ :





