Wordpressのカスタム投稿タイプを使用している時、スラッグから投稿IDを取得する方法は下記の通りです。
get_page_by_path()の第三引数にカスタム投稿タイプ名を入れる
get_page_by_path()の第三引数にカスタム投稿タイプ名を入れると、投稿のスラッグからIDを取得することができます。
$post = get_page_by_path('スラッグ' , OBJECT , 'カスタム投稿タイプ名'); $postID = $post->ID;
上記の方法で$post
にオブジェクト形式で指定したスラッグのデータを取得できます。
IDを取得する場合は$post->ID
で取得できます。
第二引数をOBJECT
にしているのでOBJECT形式で取得しますが、ARRAY_A
にすれば連想配列、ARRAY_N
にすればインデックス配列で取得できます。
取得例一覧
$post->ID //投稿ID $post->post_author //(整数) 作成者 ID $post->post_date //(文字列) 投稿日時 (YYYY-MM-DD HH:MM:SS) $post->post_date_gmt //(文字列) GMT での投稿日時 (YYYY-MM-DD HH:MM:SS) $post->post_content //(文字列) 本文 $post->post_title //(文字列) タイトル $post->post_category //(整数) カテゴリー ID。バージョン 2.1 以降、常に 0 です。投稿のカテゴリーを取得するには get_the_category() を使用してください。 $post->post_excerpt //(文字列) 抜粋 $post->post_status //(文字列) 公開ステータス (publish|pending|draft|private|static|object|attachment|inherit|future) $post->comment_status //(文字列) コメントステータス (open|closed|registered_only) $post->ping_status //(文字列) ピンバック/トラックバックステータス (open|closed) $post->post_password //(文字列) 閲覧パスワード $post->post_name //(文字列) スラッグ $post->to_ping //(文字列) ピン通知 URL $post->pinged //(文字列) ピン通知済み URL $post->post_modified //(文字列) 更新日時 (YYYY-MM-DD HH:MM:SS) $post->post_modified_gmt //(文字列) GMT での更新日時 (YYYY-MM-DD HH:MM:SS) $post->post_content_filtered //(文字列) $post->post_parent //(整数) 親 ID (固定ページや添付ファイルなどで使用) $post->guid //(文字列) 投稿へのリンクの書式になっている識別子。 $post->menu_order //(整数) 固定ページ の表示順序。 $post->post_type //(文字列) 投稿タイプ (post|page|attachment) $post->post_mime_type //(文字列) 添付ファイルのとき MIME タイプ(image/png など) $post->comment_count //(整数) コメント数
参考URL
- Wordpress関数リファレンス|get_page_by_path()