Tutorial

How to Get the Attributes of a Post Type in WordPress

The function get_post_type_object returns the attributes of a post type in WordPress.

Syntax

get_post_type_object($post_type) returns an object of type WP_Post_Type containing the attributes of the post type whose identifier is $post_type. If $post_type is not the identifier of a registered post type, NULL is returned.

// Get the object of the post type Page
$page_type_obj = get_post_type_object('page');

// Get the object of the post type Attachment
$attachment_type_obj = get_post_type_object('attachment');

Learn here how to get the attributes of various post types at once.

Attributes

The set of attributes of a post type is large. Each one is documented in detail in the class WP_Post_Type. The easiest way to see these attributes is by passing to var_dump the object returned by get_post_type_object.

// Display the attributes of the post type Page
var_dump($page_type_obj);

object(WP_Post_Type)[817]
  public 'name' => string 'page' (length=4)
  public 'label' => string 'Pages' (length=5)
  public 'labels' => 
    object(stdClass)[824]
      public 'name' => string 'Pages' (length=5)
      public 'singular_name' => string 'Page' (length=4)
      public 'add_new' => string 'Add New' (length=7)
      public 'add_new_item' => string 'Add New Page' (length=12)
      public 'edit_item' => string 'Edit Page' (length=9)
      public 'new_item' => string 'New Page' (length=8)
      public 'view_item' => string 'View Page' (length=9)
      public 'view_items' => string 'View Pages' (length=10)
      public 'search_items' => string 'Search Pages' (length=12)
      public 'not_found' => string 'No pages found.' (length=15)
      public 'not_found_in_trash' => string 'No pages found in Trash.' (length=24)
      public 'parent_item_colon' => string 'Parent Page:' (length=12)
      public 'all_items' => string 'All Pages' (length=9)
      public 'archives' => string 'Page Archives' (length=13)
      public 'attributes' => string 'Page Attributes' (length=15)
      public 'insert_into_item' => string 'Insert into page' (length=16)
      public 'uploaded_to_this_item' => string 'Uploaded to this page' (length=21)
      public 'featured_image' => string 'Featured Image' (length=14)
      public 'set_featured_image' => string 'Set featured image' (length=18)
      public 'remove_featured_image' => string 'Remove featured image' (length=21)
      public 'use_featured_image' => string 'Use as featured image' (length=21)
      public 'filter_items_list' => string 'Filter pages list' (length=17)
      public 'items_list_navigation' => string 'Pages list navigation' (length=21)
      public 'items_list' => string 'Pages list' (length=10)
      public 'menu_name' => string 'Pages' (length=5)
      public 'name_admin_bar' => string 'Page' (length=4)
  public 'description' => string '' (length=0)
  public 'public' => boolean true
  public 'hierarchical' => boolean true
  public 'exclude_from_search' => boolean false
  public 'publicly_queryable' => boolean false
  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 20
  public 'menu_icon' => null
  public 'capability_type' => string 'page' (length=4)
  public 'map_meta_cap' => boolean true
  public 'register_meta_box_cb' => null
  public 'taxonomies' => 
    array (size=0)
      empty
  public 'has_archive' => boolean false
  public 'query_var' => boolean false
  public 'can_export' => boolean true
  public 'delete_with_user' => boolean true
  public '_builtin' => boolean true
  public '_edit_link' => string 'post.php?post=%d' (length=16)
  public 'cap' => 
    object(stdClass)[819]
      public 'edit_post' => string 'edit_page' (length=9)
      public 'read_post' => string 'read_page' (length=9)
      public 'delete_post' => string 'delete_page' (length=11)
      public 'edit_posts' => string 'edit_pages' (length=10)
      public 'edit_others_posts' => string 'edit_others_pages' (length=17)
      public 'publish_posts' => string 'publish_pages' (length=13)
      public 'read_private_posts' => string 'read_private_pages' (length=18)
      public 'read' => string 'read' (length=4)
      public 'delete_posts' => string 'delete_pages' (length=12)
      public 'delete_private_posts' => string 'delete_private_pages' (length=20)
      public 'delete_published_posts' => string 'delete_published_pages' (length=22)
      public 'delete_others_posts' => string 'delete_others_pages' (length=19)
      public 'edit_private_posts' => string 'edit_private_pages' (length=18)
      public 'edit_published_posts' => string 'edit_published_pages' (length=20)
      public 'create_posts' => string 'edit_pages' (length=10)
  public 'rewrite' => boolean false
  public 'show_in_rest' => boolean true
  public 'rest_base' => string 'pages' (length=5)
  public 'rest_controller_class' => string 'WP_REST_Posts_Controller' (length=24)

Further reading

I recommend the other tutorials in this series to learn more about post types in WordPress.

Exercise

Check if the post type Post:

  1. Has archive
  2. Is hierarchical
  3. Is publicly queryable

Source code

The source code developed in this tutorial, including a possible answer to the exercise, is available here.

WordPress

Related Tutorials