---
title: "Advanced Custom Fieldsの投稿オブジェクト"
description: "前回は投稿記事に関連記事を出力する為の方法が必要だったが、 今回は、投稿オブジェ…"
url: "https://www.sensitivity.jp/wordpress/advanced-custom-fields%e3%81%ae%e6%8a%95%e7%a8%bf%e3%82%aa%e3%83%96%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88/"
canonical: "https://www.sensitivity.jp/wordpress/advanced-custom-fields%e3%81%ae%e6%8a%95%e7%a8%bf%e3%82%aa%e3%83%96%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88/"
date: "2020-07-12"
modified: "2020-08-03"
categories: ["WordPress"]
---

# Advanced Custom Fieldsの投稿オブジェクト

前回は[投稿記事に関連記事を出力する為の方法](https://www.sensitivity.jp/wordpress/advanced-custom-fields%e3%81%a7%e6%8a%95%e7%a8%bf%e8%a8%98%e4%ba%8b%e3%81%ab%e9%96%a2%e9%80%a3%e8%a8%98%e4%ba%8b%e3%82%92%e7%b4%90%e3%81%a5%e3%81%91%e3%81%a6%e8%a1%a8%e7%a4%ba/)が必要だったが、
 今回は、投稿オブジェクトを使って他の投稿記事のリンク先（パーマリンク）などを呼び出したいなど、
 先に投稿してある内容の様々な取り出しをしたいこともあり、投稿オブジェクトから拾うための 覚書メモ。

## 投稿オブジェクトを出力するため変数へセットする

```

<?php
$post_obj = get_field('custm_field');
?>
```

### 投稿オブジェクト詳細の出力

```

//タイトル
<?=$post_obj->post_title?>

//パーマリンク
<?=get_the_permalink($post_obj->ID)?>

//アイキャッチ
<?=get_the_post_thumbnail( $post_obj->ID,'サイズ')?>

//投稿日時
<?=get_the_date('Y.m.d',$post_obj->ID)?>

//所属しているカテゴリ
<php
$categorys = get_the_category($post_obj->ID);
$category = $categorys[0];
$cat_name = $category->cat_name;
?>
<?=$cat_name?>
```

### 投稿オブジェクトの返り値

```

object(WP_Post) {
  ["ID"]                    => int(123) "投稿 ID"
  ["post_author"]           => string() "作成者ID"
  ["post_date"]             => string() "投稿日時 (YYYY-MM-DD HH:MM:SS)"
  ["post_date_gmt"]         => string() "GMT での投稿日時 (YYYY-MM-DD HH:MM:SS)"
  ["post_content"]          => string() "コンテンツ本文"
  ["post_title"]            => string() "タイトル"
  ["post_excerpt"]          => string() "抜粋"
  ["post_status"]           => string() "公開ステータス"
  ["comment_status"]        => string() "コメントステータス"
  ["ping_status"]           => string() "ピンバック／トラックバックステータス"
  ["post_password"]         => string() "閲覧パスワード"
  ["post_name"]             => string() "スラッグ"
  ["to_ping"]               => string() "ピン通知 URL"
  ["pinged"]                => string() "ピン通知済み URL"
  ["post_modified"]         => string() "更新日時 (YYYY-MM-DD HH:MM:SS)"
  ["post_modified_gmt"]     => string() "GMT での更新日時 (YYYY-MM-DD HH:MM:SS)"
  ["post_content_filtered"] => string() ""
  ["post_parent"]           => int()    "親 ID (固定ページや添付ファイルなどで使用)"
  ["guid"]                  => string() "http://example.com/?p=123"
  ["menu_order"]            => int()    "固定ページ の表示順序。"
  ["post_type"]             => string() "投稿タイプ"
  ["post_mime_type"]        => string() "添付ファイルのとき MIME タイプ"
  ["comment_count"]         => string() "コメント数"
  ["filter"]                => string() "raw"
}
```

これらで簡単に投稿オブジェクトの内容を任意の場所で使用することが出来る。

### おまけ：変数が空の場合

```

<?php if (get_field('$post_obj->ID')):
echo '値がある場合の内容を記述';
else :
 echo '値がない場合（非表示なら何もかかなくていい）';
endif; ?>
```

※これ、つい設定するの忘れて値が空欄のときに「あぁ〜！！」ってなるやつです。

  [ACF：Post Object](https://www.advancedcustomfields.com/resources/post-object/)

