LoginSignup
108

More than 5 years have passed since last update.

【WordPress】スラッグ名からIDを取得する方法

Last updated at Posted at 2014-07-12

すぐに忘れてしまうのでメモ。
スラッグからIDを取得したいときに使える。

固定ページ

hoge.php
$get_page_id = get_page_by_path("sulg_name");
$get_page_id = $get_page_id->ID;

カテゴリー

hoge.php
$cat_id = get_category_by_slug("sulg_name");
$cat_id = $cat_id->cat_ID;

カスタムタクソノミーは上記の記述では取得できないので、 get_term_by() を使う。

$term    = get_term_by('slug', 'slug_name', 'custom_taxonomy')
$term_id = $term->term_id;

タグ

$tag    = get_tags(array('slug' => 'slug_name'));
$tag_id = $tag[0]->term_id;

投稿

hoge.php
$post_id = get_posts("name=sulg_name");
$post_id = $post_id[0]->ID;

※ 追記(16/02/08)
投稿もget_page_by_pathを使って取得出来るとの指摘を頂きました。

hoge.php
$post_id = get_page_by_path("sulg_name", "OBJECT", "post");
$post_id = $post_id->ID;

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
108