WEB制作者向け無料テーマ「Arkhe」をリリースしました

WordPressの各種ページでget_queried_object()から取得できる値の一覧

get_queried_object() とは、現在リクエストされているクエリの情報オブジェクトを取得する関数です。

そのページが投稿ページなのか、アーカイブページなのか、などによって 取得できる情報オブジェクトが変化します。

使いこなせてくると 非常〜〜に便利な関数で、一度 wpアクションフック などでget_queried_object()の内容を取得しておけば、各ページでget_title()get_post_type()などで情報オブジェクトをいちいち呼び出す必要がなくなり、高速化にも繋がります。

そこで、今回はこのget_queried_object()によって取得できる情報がどういうものか、各ページ種別ごとに整理していきます。

目次

投稿ページ・固定ページなどで取得できる情報

各種投稿ページ(固定ページ・カスタム投稿ページ・アタッチメントページも含む)では、WP_Post オブジェクトが取得できます。

条件分岐タグでいうと is_singular() が true となるページですね。

さて、実際に取得できる情報を吐き出してみましょう。

投稿ページ(single.php)で var_dump()してみる

var_dump( get_queried_object() );

出力結果(初期投稿 Hello world! のページで確認)

object(WP_Post) {
  public 'ID' => int 1
  public 'post_author' => string '1'
  public 'post_date' => string '2018-10-22 05:38:50'
  public 'post_date_gmt' => string '2018-10-22 05:38:50'
  public 'post_content' => string 'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!'
  public 'post_title' => string 'Hello world!'
  public 'post_excerpt' => string ''
  public 'post_status' => string 'publish'
  public 'comment_status' => string 'open'
  public 'ping_status' => string 'open'
  public 'post_password' => string ''
  public 'post_name' => string 'hello-world'
  public 'to_ping' => string ''
  public 'pinged' => string ''
  public 'post_modified' => string '2018-11-19 06:34:14'
  public 'post_modified_gmt' => string '2018-11-19 06:34:14'
  public 'post_content_filtered' => string ''
  public 'post_parent' => int 0
  public 'guid' => string 'http://my-theme.wp/?p=1'
  public 'menu_order' => int 0
  public 'post_type' => string 'post'
  public 'post_mime_type' => string ''
  public 'comment_count' => string '1'
  public 'filter' => string 'raw'
}

このように、表示中の投稿の情報が保存されたWP_Postというオブジェクトが取得できます。

アーカイブページで取得できる情報

次はアーカイブページでも確認していきましょう。

ただ、アーカイブページはいくつかの種類に分かれていますので、それぞれのページで確認していきます。

カテゴリー・タグ・タクソノミーアーカイブ

各ターム系の一覧ページでは、WP_Term オブジェクトが取得できます。

条件分岐タグでいうとis_category() || is_tag() || is_tax() が true となるページです。

先ほどと同じように、var_dump() で確認してみました。

出力結果(newsというタームに関するカテゴリーページで確認)

object(WP_Term) {
  public 'term_id' => int 1
  public 'name' => string 'ニュース'
  public 'slug' => string 'news'
  public 'term_group' => int 0
  public 'term_taxonomy_id' => int 1
  public 'taxonomy' => string 'category'
  public 'description' => string ''
  public 'parent' => int 0
  public 'count' => int 2
  public 'filter' => string 'raw'
  public 'cat_ID' => int 1
  public 'category_count' => int 2
  public 'category_description' => string ''
  public 'cat_name' => string 'ニュース'
  public 'category_nicename' => string 'news'
  public 'category_parent' => int 0
}

'cat_ID' ~ 'category_parent' というプロパティはカテゴリーアーカイブページでのみ存在します。

投稿タイプアーカイブ

カスタム投稿タイプのアーカイブページでは、WP_Post_Type オブジェクトが取得できます。

条件分岐タグでいうと is_post_type_archive() が true になるページですね。

出力結果(「商品」というカスタム投稿タイプのアーカイブページにて確認)

object(WP_Post_Type) {
  public 'name' => string 'item'
  public 'label' => string '商品'
  public 'labels' => object(stdClass) {
    public 'name' => string '商品'
    public 'singular_name' => string '商品'
    public 'add_new' => string '新規追加'
    public 'add_new_item' => string '新規投稿を追加'
    public 'edit_item' => string '投稿の編集'
    public 'new_item' => string '新規投稿'
    public 'view_item' => string '投稿を表示'
    public 'view_items' => string '投稿の表示'
    public 'search_items' => string '投稿を検索'
    public 'not_found' => string '投稿が見つかりませんでした。'
    public 'not_found_in_trash' => string 'ゴミ箱内に投稿が見つかりませんでした。'
    public 'parent_item_colon' => null
    public 'all_items' => string '商品'
    public 'archives' => string '商品'
    public 'attributes' => string '投稿の属性'
    public 'insert_into_item' => string '投稿に挿入'
    public 'uploaded_to_this_item' => string 'この投稿へのアップロード'
    public 'featured_image' => string 'アイキャッチ画像'
    public 'set_featured_image' => string 'アイキャッチ画像を設定'
    public 'remove_featured_image' => string 'アイキャッチ画像を削除'
    public 'use_featured_image' => string 'アイキャッチ画像として使用'
    public 'filter_items_list' => string '投稿リストの絞り込み'
    public 'items_list_navigation' => string '投稿リストナビゲーション'
    public 'items_list' => string '投稿リスト'
    public 'menu_name' => string '商品'
    public 'name_admin_bar' => string '商品'
  }
  public 'description' => string ''
  public 'public' => boolean true
  public 'hierarchical' => boolean false
  public 'exclude_from_search' => boolean false
  public 'publicly_queryable' => boolean true
  public 'show_ui' => boolean true
  public 'show_in_menu' => boolean true
  public 'show_in_nav_menus' => boolean true
  public 'show_in_admin_bar' => boolean true
  public 'menu_position' => int 6
  public 'menu_icon' => null
  public 'capability_type' => string 'post'
  public 'map_meta_cap' => boolean true
  public 'register_meta_box_cb' => null
  public 'taxonomies' => array (0) empty
  public 'has_archive' => boolean true
  public 'query_var' => string 'item'
  public 'can_export' => boolean true
  public 'delete_with_user' => null
  public '_builtin' => boolean false
  public '_edit_link' => string 'post.php?post=%d'
  public 'cap' => object(stdClass) {
    public 'edit_post' => string 'edit_post'
    public 'read_post' => string 'read_post'
    public 'delete_post' => string 'delete_post'
    public 'edit_posts' => string 'edit_posts'
    public 'edit_others_posts' => string 'edit_others_posts'
    public 'publish_posts' => string 'publish_posts'
    public 'read_private_posts' => string 'read_private_posts'
    public 'read' => string 'read'
    public 'delete_posts' => string 'delete_posts'
    public 'delete_private_posts' => string 'delete_private_posts'
    public 'delete_published_posts' => string 'delete_published_posts'
    public 'delete_others_posts' => string 'delete_others_posts'
    public 'edit_private_posts' => string 'edit_private_posts'
    public 'edit_published_posts' => string 'edit_published_posts'
    public 'create_posts' => string 'edit_posts'
  }
  public 'rewrite' => array (5) {
    'slug' => string 'item'
    'with_front' => boolean true
    'pages' => boolean true
    'feeds' => boolean true
    'ep_mask' => int 1
  }
  public 'show_in_rest' => boolean false
  public 'rest_base' => boolean false
  public 'rest_controller_class' => boolean false
}

日付アーカイブ

日付アーカイブページでは、null が返ってきます。

条件分岐タグでいうと is_date() が true になるページですね。

日付情報はあくまで投稿に紐づく子情報のようです。

投稿者アーカイブ

投稿者アーカイブページでは、WP_User オブジェクトが取得できます。

条件分岐タグでいうと is_author() が true になるページですね。

object(WP_User){
  public 'data' => object(stdClass) {
    public 'ID' => string '1'
    public 'user_login' => string 'admin'
    public 'user_pass' => string 'ハッシュ化されたパスワード'
    public 'user_nicename' => string 'admin'
    public 'user_email' => string 'dev-email@flywheel.local'
    public 'user_url' => string ''
    public 'user_registered' => string '2018-10-22 05:38:50'
    public 'user_activation_key' => string ''
    public 'user_status' => string '0'
    public 'display_name' => string 'loos'
  }
  public 'ID' => int 1
  public 'caps' => array (1) {
    'administrator' => boolean true
  }
  public 'cap_key' => string 'wp_capabilities'
  public 'roles' => array (1) {
    0 => string 'administrator'
  }
  public 'allcaps' => array (62) {
    'switch_themes' => boolean true
    'edit_themes' => boolean true
    'activate_plugins' => boolean true
    'edit_plugins' => boolean true
    'edit_users' => boolean true
    'edit_files' => boolean true
    'manage_options' => boolean true
    'moderate_comments' => boolean true
    'manage_categories' => boolean true
    'manage_links' => boolean true
    'upload_files' => boolean true
    'import' => boolean true
    'unfiltered_html' => boolean true
    'edit_posts' => boolean true
    'edit_others_posts' => boolean true
    'edit_published_posts' => boolean true
    'publish_posts' => boolean true
    'edit_pages' => boolean true
    'read' => boolean true
    'level_10' => boolean true
    'level_9' => boolean true
    'level_8' => boolean true
    'level_7' => boolean true
    'level_6' => boolean true
    'level_5' => boolean true
    'level_4' => boolean true
    'level_3' => boolean true
    'level_2' => boolean true
    'level_1' => boolean true
    'level_0' => boolean true
    'edit_others_pages' => boolean true
    'edit_published_pages' => boolean true
    'publish_pages' => boolean true
    'delete_pages' => boolean true
    'delete_others_pages' => boolean true
    'delete_published_pages' => boolean true
    'delete_posts' => boolean true
    'delete_others_posts' => boolean true
    'delete_published_posts' => boolean true
    'delete_private_posts' => boolean true
    'edit_private_posts' => boolean true
    'read_private_posts' => boolean true
    'delete_private_pages' => boolean true
    'edit_private_pages' => boolean true
    'read_private_pages' => boolean true
    'delete_users' => boolean true
    'create_users' => boolean true
    'unfiltered_upload' => boolean true
    'edit_dashboard' => boolean true
    'update_plugins' => boolean true
    'delete_plugins' => boolean true
    'install_plugins' => boolean true
    'update_themes' => boolean true
    'install_themes' => boolean true
    'update_core' => boolean true
    'list_users' => boolean true
    'remove_users' => boolean true
    'promote_users' => boolean true
    'edit_theme_options' => boolean true
    'delete_themes' => boolean true
    'export' => boolean true
    'administrator' => boolean true
  }
  public 'filter' => null
  private 'site_id' => int 1
}

トップページで取得できる情報

これは管理画面で設定できる、「ホームページの表示」設定によって変わります。

  • 「最新の投稿」に設定してある場合、get_queried_object() の返り値は null となります。
  • 「固定ページ」に設定されてある場合、指定された固定ページに関する WP_Post オブジェクト となります。

「ホームページの表示」設定による各種挙動の違いに関して、以下の記事で詳しくまとめていますのでぜひ参考にしてみてください。

あわせて読みたい
WordPressで「ホームページの表示」設定で最新の投稿・固定ページを指定した時のトップページの挙動の違... WordPressの管理メニュー設定項目の中に「ホームページの表示」という設定がありますよね。 サイトのトップページに関する重要な設定ですが、これによってどこがどう変...

 

その他のページで取得できる情報

その他のページに関しては、以下のようになります。

  • 検索結果ページ:null
  • 404ページ:null

検索結果ページは何も取得できないようですね。

まとめ

最後に、分かったことをまとめておきます。

ページ種別 get_queried_object() の返り値
トップページ null または WP_Post
投稿系ページ WP_Post
ターム系アーカイブ WP_Term
投稿タイプアーカイブ WP_Post_Type
日付アーカイブ null
投稿者アーカイブ WP_User
検索結果ページ null
404ページ null

この記事が気に入ったら
フォローしてね!

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次