get_post_type_object()WP 3.0.0

Retrieves a post type object by name.

1 time — 0.000068 sec (very fast) | 50000 times — 2.36 sec (fast) | PHP 7.0.5, WP 4.5.2

No Hooks.

Return

WP_Post_Type|null. WP_Post_Type object if it exists, null otherwise.

Usage

get_post_type_object( $post_type );
$post_type(string) (required)
The name of a registered post type.

Examples

0

#1 Get the name of the custom post type

To display, get the localized name of the post type:

$obj = get_post_type_object( 'post' );

echo $obj->labels->singular_name;

It is the same as:

global $wp_post_types;
$obj = $wp_post_types['post'];

echo $obj->labels->singular_name;
0

#2 What the object looks like

For illustration: The object of the basic post type "post" looks like this:

$obj = get_post_type_object( 'post' );

/* $obj will contain:

stdClass Object
(
	[labels] => stdClass Object
		(
			[name] => Posts
			[singular_name] => Post
			[add_new] => Add new
			[add_new_item] => Add post
			[edit_item] => Edit post
			[new_item] => New post
			[view_item] => View post
			[search_items] => Search for posts
			[not_found] => No posts found.
			[not_found_in_trash] => No posts in the cart found.
			[parent_item_colon] =>
			[all_items] => All posts
			[menu_name] => Posts
			[name_admin_bar] => Post
		)

	[description] =>
	[public] => 1
	[hierarchical] =>
	[exclude_from_search] =>
	[publicly_queryable] => 1
	[show_ui] => 1
	[show_in_menu] => 1
	[show_in_nav_menus] => 1
	[show_in_admin_bar] => 1
	[menu_position] =>
	[menu_icon] =>
	[capability_type] => post
	[map_meta_cap] => 1
	[register_meta_box_cb] =>
	[taxonomies] => Array
		(
		)

	[has_archive] =>
	[rewrite] =>
	[query_var] =>
	[can_export] => 1
	[delete_with_user] => 1
	[_builtin] => 1
	[_edit_link] => post.php?post=%d
	[name] => post
	[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
		)

	[label] => Posts
)
*/

Note that the object’s attribute names are slightly different than the arguments expected for the register_post_type() function.

Notes

Changelog

Since 3.0.0 Introduced.
Since 4.6.0 Object returned is now an instance of WP_Post_Type.

get_post_type_object() code WP 6.5.2

function get_post_type_object( $post_type ) {
	global $wp_post_types;

	if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) {
		return null;
	}

	return $wp_post_types[ $post_type ];
}