wppaste
WordPress

get_post_type( int|WP_Post|null $post = null ): string|false

Since
2.1.0
Source
wp-includes/post.php:1596

Resolves the post type slug for a given post ID, WP_Post object, or the current global $post when no argument is passed. Returns false if the post cannot be found, so check the return value before treating it as a string. Pairs well with get_post_type_object() when you need labels or capabilities instead of just the slug.

Retrieves the post type of the current post or of a given post.

Compatibility

WordPress
since 2.1.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

$postint|WP_Post|nulloptional
Post ID or post object. Default is global $post.Default: null

Return value

string|false
Post type on success, false on failure.

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.

Get the post type for a specific post ID

Compare the type of a regular post against the fixture page to see the string each returns.

$post_type = get_post_type( 1 );
printf( 'Post 1 is of type: %s' . "\n", esc_html( $post_type ) );

$page_type = get_post_type( 6 );
printf( 'Post 6 is of type: %s' . "\n", esc_html( $page_type ) );

$missing = get_post_type( 999 );
if ( false === $missing ) {
	echo 'Post 999 does not exist, get_post_type() returned false' . "\n";
}

Detect the post type inside The Loop without passing an ID

Run a small query and call get_post_type() with no argument so it falls back to the global $post set by the_post().

$query = new WP_Query( array(
	'post_type'      => 'any',
	'posts_per_page' => 4,
) );

if ( $query->have_posts() ) {
	while ( $query->have_posts() ) {
		$query->the_post();
		printf( 'Post ID %d is a %s' . "\n", get_the_ID(), esc_html( get_post_type() ) );
	}
	wp_reset_postdata();
} else {
	echo 'No posts found.' . "\n";
}

wp_reset_postdata() is needed after the custom loop so the global $post is restored to whatever it was before.

Common problems and fixes · 4

Why does get_post_type() return false instead of a string?

The function calls get_post() internally and only returns post_type if that lookup succeeds. A nonexistent post ID, a deleted post, or an empty global $post all make get_post() return null, so get_post_type() falls through to false.

Why does get_post_type() give the wrong result when I call it with no arguments?

With no argument the function reads the global $post, so its output depends entirely on what the current query or template has set that global to at the moment you call it.

Can I pass a WP_Post object instead of an ID?

Yes. The parameter accepts an int, a WP_Post object, or null, because get_post() normalizes any of those into a WP_Post before the post_type property is read.

Is it safe to use the return value directly in a database query or URL?

The function returns the raw post_type slug as stored on the post object, with no escaping applied.

Alternatives and related functions

get_post_type_object
When you need the full post type object with labels and capabilities rather than just the slug string.
get_post_types
When you need the list of all registered post type names, not the type of one particular post.
post_type_exists
When you only need to confirm a post type slug is registered, without loading a specific post.
is_singular
When you are inside a template and want a boolean check against the currently queried post type instead of the slug itself.

Performance profile

How much work a call to get_post_type() 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
Heavy

Reaches the database via get_post().

Scaling
Constant

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

Instructions
7–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
49

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

What it touches

  • querycontent queryget_post()called directly

Further down the call graph this can also reach hook, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

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() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always7–8get_post()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 9 instructions, 7–8 executed per call, 1 branch. The work does not change between versions.

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.

Uses · 1

  • get_post()Retrieves post data given a post ID or post object.

Used by · 49

Show all 49

Source code

function get_post_type( $post = null ) {	$post = get_post( $post );	if ( $post ) {		return $post->post_type;	} 	return false;}

Changelog

Introduced in 2.1.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.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.