wppaste
WordPress

get_post_type_object( string $post_type ): WP_Post_Type|null

Since
3.0.0, 4.6.0
Source
wp-includes/post.php:1596

Look up the WP_Post_Type object registered for a given post type name, returning null if that type isn't registered. It reads directly from the global $wp_post_types array populated by register_post_type(), so it only sees types already registered on the current request. Pair it with post_type_exists() when you only need a true/false check rather than the full object.

Retrieves a post type object by name.

Compatibility

WordPress
since 4.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$post_typestring
The name of a registered post type.

Return value

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

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Check whether a post type is hierarchical before building a menu

Inspect the built-in 'page' post type's properties to decide how to render an admin listing.

$post_type_object = get_post_type_object( 'page' );

if ( $post_type_object ) {
	printf(
		'The "page" post type is %s and %s show up in the admin menu.',
		esc_html( $post_type_object->hierarchical ? 'hierarchical' : 'non-hierarchical' ),
		esc_html( $post_type_object->show_in_menu ? 'will' : 'will not' )
	);
} else {
	echo 'No such post type is registered.';
}

Read the admin labels of a custom post type

Register a 'book' post type and pull its label object to reuse the singular and plural names elsewhere.

$book_type = get_post_type_object( 'book' );

if ( $book_type ) {
	echo esc_html( sprintf( 'Singular label: %s | Plural label: %s', $book_type->labels->singular_name, $book_type->labels->name ) );
} else {
	echo 'The book post type is not registered.';
}

Common problems and fixes · 4

Why does get_post_type_object() return null for my custom post type?

The function looks the name up in the global $wp_post_types array, which register_post_type() only populates once it runs, usually on the 'init' hook. If you call get_post_type_object() before that hook fires, or misspell the slug, the array key is empty and null is returned.

Can I pass an array of post type names to get_post_type_object()?

No. The source checks is_scalar( $post_type ) first and returns null immediately for anything that isn't a scalar, so an array short-circuits the lookup.

How do I get the object for every registered post type, not just one?

get_post_type_object() only accepts one name at a time by design. For a full list, reach for the sibling function that returns names or objects for every registered type.

If I change a property on the object I get back, does it affect the rest of the request?

Yes. The function returns the same WP_Post_Type instance stored in $wp_post_types (PHP passes objects by handle), so mutating a property on the returned object changes it globally for the remainder of the request, affecting any later code that reads that post type.

Alternatives and related functions

post_type_exists
When you only need a true or false answer about whether a post type is registered, not the object itself.
get_post_types
When you need the names or objects for every registered post type instead of one specific type.
get_post_type
When you have a post ID or WP_Post object and just want the post type slug it belongs to.
register_post_type
When the post type doesn't exist yet and you need to define it before you can look it up.

Performance profile

How much work a call to get_post_type_object() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5–8

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 9.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_post_type_object() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always5–8none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev95–82
8.595–82
8.495–82
8.395–82
8.295–82
8.195–822 fewer instructions than PHP 7.4
7.4117–102

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Used by · 50

Show all 50

Source code

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 ];}

Changelog

Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.6.0
Object returned is now an instance of WP_Post_Type.from the docblock
3.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.