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);
Further reading
I recommend the other tutorials in this series to learn more about post types in WordPress.
- Post Types in WordPress
- How to Register a Custom Post Type in WordPress
- How to Register a Custom Post Type Using a Plugin in WordPress
- How to Unregister a Custom Post Type in WordPress
- How to Modify a Post Type in WordPress
- How to Change the Slug of a Post Type in WordPress
- How to Check if a Post Type Exists in WordPress
- How to Get the Registered Post Types in WordPress
- How to Get the Attributes of a Post Type in WordPress
- How to Get the URL of a Post Type Archive in WordPress
- How to Add Custom Post Types to the Main Query in WordPress
Exercise
Check if the post type Post:
- Has archive
- Is hierarchical
- Is publicly queryable
Source code
The source code developed in this tutorial, including a possible answer to the exercise, is available here.
Comments