• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

関数リファレンス/get post type object

提供: WordPress Codex 日本語版
移動先: 案内検索

登録済み投稿タイプを表すオブジェクトを取得します。 初めから組み込まれている 'post'(投稿)、'page'(固定ページ)等や、カスタム投稿タイプも対象です。

使い方

<?php get_post_type_object( $post_type ); ?>

パラメータ

$post_type
文字列) (必須) 投稿タイプを登録したときに使った、投稿タイプの名前。
初期値: なし
参考:register_post_type()$post_type パラメータで指定した値です。

戻り値

(オブジェクト) 
成功したときのみ。

失敗すると何も返しません(null かどうかでチェックできます)。

用例

$obj = get_post_type_object( 'post' );
echo $obj->labels->singular_name;

技術的には、上記コードは下記と同じです:

global $wp_post_types;
$obj = $wp_post_types['post'];
echo $obj->labels->singular_name; 

print_r( $obj ) を実行すると例えば次のような値が返されます:

 stdClass Object
 (
    [labels] => stdClass Object
        (
            [name] => Posts
            [singular_name] => Post
            [add_new] => Add New
            [add_new_item] => Add New Post
            [edit_item] => Edit Post
            [new_item] => New Post
            [view_item] => View Post
            [search_items] => Search Posts
            [not_found] => No posts found
            [not_found_in_trash] => No posts found in Trash
            [parent_item_colon] => 
        )
 
    [description] => 
    [publicly_queryable] => 1
    [exclude_from_search] => 
    [_builtin] => 1
    [_edit_link] => post.php?post=%d
    [capability_type] => post
    [hierarchical] => 
    [public] => 1
    [rewrite] => 
    [query_var] => 
    [register_meta_box_cb] => 
    [taxonomies] => Array
        (
        )
 
    [show_ui] => 1
    [menu_position] => 
    [menu_icon] => 
    [permalink_epmask] => 1
    [can_export] => 1
    [show_in_nav_menus] => 1
    [name] => post
    [cap] => stdClass Object
        (
            [edit_post] => edit_post
            [edit_posts] => edit_posts
            [edit_others_posts] => edit_others_posts
            [publish_posts] => publish_posts
            [read_post] => read_post
            [read_private_posts] => read_private_posts
            [delete_post] => delete_post
        )
 
    [label] => Posts
 )

投稿タイプ 'certification'を持っていると仮定すると、以下のようにできます:

$obj = get_post_type_object( 'certification' );

以下のように、print_r( $obj ) を返す場合もあります:

 stdClass Object
 (
     [labels] => stdClass Object
         (
             [name] => Certification
             [singular_name] => Certification
             [add_new] => Add New
             [add_new_item] => Add New Certification
             [edit_item] => Edit Certification
             [new_item] => New Page
             [view_item] => View Certification
             [search_items] => Search Certification
             [not_found] => Not found
             [not_found_in_trash] => Not found in Trash
             [parent_item_colon] => Parent Certification:
             [all_items] => All Certifications
             [menu_name] => Certifications
             [update_item] => Update Certification
             [name_admin_bar] => Certification
         )
 
     [description] => Certifications
     [public] => 1
     [hierarchical] => 1
     [exclude_from_search] => 
     [publicly_queryable] => 1
     [show_ui] => 1
     [show_in_menu] => 
     [show_in_nav_menus] => 1
     [show_in_admin_bar] => 1
     [menu_position] => 5
     [menu_icon] => dashicons-welcome-widgets-menus
     [capability_type] => post
     [map_meta_cap] => 1
     [register_meta_box_cb] => 
     [taxonomies] => Array
         (
             [0] => objective
         )
 
     [has_archive] => 1
     [rewrite] => Array
         (
             [slug] => certification
             [with_front] => 1
             [pages] => 1
             [feeds] => 1
             [ep_mask] => 1
         )
 
     [query_var] => certification
     [can_export] => 1
     [delete_with_user] => 
     [_builtin] => 
     [_edit_link] => post.php?post=%d
     [label] => Certification
     [name] => certification
     [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
         )
 
 )

注意:オブジェクトの属性の名前が register_post_type() の引数の名前と少し違っています。

アーカイブページでカスタム投稿タイプのスラッグを取得

<?php
if ( is_post_type_archive() ) {
  $post_type = get_query_var( 'post_type' );
  if ( is_array( $post_type ) )
    $post_type = reset( $post_type );
  $post_type_obj = get_post_type_object( $post_type );
  echo "カスタム投稿タイプ $post_type_obj->labels->name のスラッグは $post_type_obj->name です。";
}
?>

更新履歴

  • 3.0 から導入されました。

ソースファイル

get_post_type_object()wp-includes/post.php にあります。


投稿タイプ: register_post_type(), add_post_type_support(), remove_post_type_support(), post_type_supports(), post_type_exists(), set_post_type(), get_post_type(), get_post_types(), get_post_type_object(), get_post_type_capabilities(), get_post_type_labels(), is_post_type_hierarchical(), is_post_type_archive(), post_type_archive_title()


関数リファレンステンプレートタグ目次もご覧ください。


最新英語版: WordPress Codex » Function_Reference/get_post_type_object最新版との差分