Blog

WordPress : get_queried_object()の返り値のまとめ

Posted by admin at 7:54 日時 2013/09/11

WordPressでfunctions.phpなどあらかじめ分岐されたテンプレートではないPHPでクエリーオブジェクトを取得して何かしたいときに、いつもget_queried_objectの挙動が不安なのでメモってみた。

※3.6時点

  • ホーム:NULL
  • 投稿シングル:WP_Postオブジェクト
WP_Post Object  (      [ID] => 1      [post_author] => 1      [post_date] => 2013-08-24 05:57:40      [post_date_gmt] => 2013-08-24 05:57:40      [post_content] => WordPress へようこそ。これは最初の投稿です。編集もしくは削除してブログを始めてください !      [post_title] => Hello world!      [post_excerpt] =>       [post_status] => publish      [comment_status] => open      [ping_status] => open      [post_password] =>       [post_name] => hello-world      [to_ping] =>       [pinged] =>       [post_modified] => 2013-09-11 07:07:54      [post_modified_gmt] => 2013-09-10 22:07:54      [post_content_filtered] =>       [post_parent] => 0      [guid] => http://localhost/wordpress/?p=1      [menu_order] => 0      [post_type] => post      [post_mime_type] =>       [comment_count] => 1      [filter] => raw      [format_content] =>   )
  • 固定ページ:WP_Postオブジェクト
WP_Post Object  (      [ID] => 2      [post_author] => 1      [post_date] => 2013-08-24 05:57:40      [post_date_gmt] => 2013-08-24 05:57:40      [post_content] => これはサンプルページです。      [post_title] => サンプルページ      [post_excerpt] =>       [post_status] => publish      [comment_status] => open      [ping_status] => open      [post_password] =>       [post_name] => sample-page      [to_ping] =>       [pinged] =>       [post_modified] => 2013-08-24 05:57:40      [post_modified_gmt] => 2013-08-24 05:57:40      [post_content_filtered] =>       [post_parent] => 0      [guid] => http://localhost/wordpress/?page_id=2      [menu_order] => 0      [post_type] => page      [post_mime_type] =>       [comment_count] => 0      [filter] => raw      [format_content] =>   )
  • カテゴリーアーカイブ:stdClassオブジェクト
stdClass Object  (      [term_id] => 1      [name] => 未分類      [slug] => %e6%9c%aa%e5%88%86%e9%a1%9e      [term_group] => 0      [term_taxonomy_id] => 1      [taxonomy] => category      [description] =>       [parent] => 0      [count] => 3      [cat_ID] => 1      [category_count] => 3      [category_description] =>       [cat_name] => 未分類      [category_nicename] => %e6%9c%aa%e5%88%86%e9%a1%9e      [category_parent] => 0  )
  • タグアーカイブ:stdClassオブジェクト
stdClass Object  (      [term_id] => 3      [name] => タグ      [slug] => %e3%82%bf%e3%82%b0      [term_group] => 0      [term_taxonomy_id] => 3      [taxonomy] => post_tag      [description] =>       [parent] => 0      [count] => 1  )
  • 日付アーカイブ:NULL
  • 添付ファイル:WP_Postオブジェクト
WP_Post Object  (      [ID] => 57      [post_author] => 1      [post_date] => 2013-09-11 07:14:15      [post_date_gmt] => 2013-09-10 22:14:15      [post_content] =>       [post_title] => IMG_0118      [post_excerpt] =>       [post_status] => inherit      [comment_status] => open      [ping_status] => open      [post_password] =>       [post_name] => img_0118      [to_ping] =>       [pinged] =>       [post_modified] => 2013-09-11 07:14:15      [post_modified_gmt] => 2013-09-10 22:14:15      [post_content_filtered] =>       [post_parent] => 0      [guid] => http://localhost/wordpress/wp-content/uploads/2013/09/IMG_0118.jpg      [menu_order] => 0      [post_type] => attachment      [post_mime_type] => image/jpeg      [comment_count] => 0      [filter] => raw      [format_content] =>   )
  • 検索結果:NULL
  • 404:NULL
  • 投稿タイプアーカイブ:stdClassオブジェクト
stdClass Object  (      [labels] => stdClass Object          (              [name] => dog              [singular_name] => dog              [add_new] => Add dog              [add_new_item] => Add New dog              [edit_item] => Edit dog              [new_item] => New dog              [view_item] => View dog              [search_items] => Search dog              [not_found] => No dog Found              [not_found_in_trash] => No dog Found in Trash              [parent_item_colon] =>               [all_items] => dog              [menu_name] => dog              [edit] => Edit              [view] => View dog              [parent] => Parent dog              [name_admin_bar] => dog          )        [description] =>       [publicly_queryable] => 1      [exclude_from_search] =>       [capability_type] => post      [map_meta_cap] => 1      [_builtin] =>       [_edit_link] => post.php?post=%d      [hierarchical] =>       [public] => 1      [rewrite] => Array          (              [slug] => dog              [with_front] => 1              [pages] => 1              [feeds] => 1              [ep_mask] => 1          )        [has_archive] => 1      [query_var] => dog      [register_meta_box_cb] =>       [taxonomies] => Array          (              [0] => category          )        [show_ui] => 1      [menu_position] =>       [menu_icon] =>       [can_export] => 1      [show_in_nav_menus] => 1      [show_in_menu] => 1      [show_in_admin_bar] => 1      [delete_with_user] =>       [label] => dog      [singular_label] => dog      [name] => dog      [cap] => stdClass Object          (              [edit_post] => edit_post              [read_post] => read_post              [delete_post] => delete_post              [edit_posts] => edit_posts              [edit_others_posts] => edit_others_posts              [publish_posts] => publish_posts              [read_private_posts] => read_private_posts              [read] => read              [delete_posts] => delete_posts              [delete_private_posts] => delete_private_posts              [delete_published_posts] => delete_published_posts              [delete_others_posts] => delete_others_posts              [edit_private_posts] => edit_private_posts              [edit_published_posts] => edit_published_posts              [create_posts] => edit_posts          )    )
  • タクソノミーアーカイブ:stdClassオブジェクト
stdClass Object  (      [term_id] => 4      [name] => Jason Statham      [slug] => jason-statham      [term_group] => 0      [term_taxonomy_id] => 4      [taxonomy] => actors      [description] =>       [parent] => 0      [count] => 0  )

所感

  • WP_Postオブジェクトは便利だ。post_typeパラメーターで見分けがつく
  • カテゴリーだけ後方互換性のためかオブジェクトの内容が違う。重複しているだけなので category_nicename や cat_name パラメーターは使わないほうが汎用性が高い
  • stdClassで返ってくるのは今のところタクソノミーと投稿タイプの2パターンがあり得る。stdClassやめてくれマジで
  • タクソノミーが絡まないアーカイブや検索結果がNULLなのはこういうもんだと思うしかなさそうだ
  • get_queried_object_id() の返り値は、post_id または term_id でそれ以外は0。投稿タイプアーカイブは get_queried_object() だとオブジェクトが返ってくるが get_queried_object_id() は無反応なので注意

ということで考えられる妥当な条件分岐(なげえ)

$obj = get_queried_object();    if (is_object($obj)) {  	if ($obj instanceof WP_Post) {  		echo '投稿';  		if ( is_front_page() ) {  			echo '表示設定で選択したフロントページ';  		} elseif( is_home() ) {  			echo '表示設定で選択した投稿ページ';  		}  	} elseif ($obj instanceof stdClass) { // ここ何とかして!  		if ( isset($obj->taxonomy) ) {  			echo 'タクソノミー';  		} elseif ( isset($obj->labels) ) {  			echo '投稿タイプ';  		}  	}  } elseif( is_archive() ) {  	echo 'タクソノミー、投稿タイプ以外のアーカイブ';  } elseif( is_search() ) {  	echo '検索結果';  } elseif( is_home() ) {  	echo '通常のホーム';  } else {  	echo '404';  }

うーん、素直に条件分岐関数を使ったほうがマシっぽいですな。


Share this entry

Blog Entry Topics