Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
105
Help us understand the problem. What is going on with this article?
@konweb

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

More than 5 years have passed since last update.

すぐに忘れてしまうのでメモ。
スラッグから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;
105
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
konweb

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
105
Help us understand the problem. What is going on with this article?