get_post_type( int|WP_Post|null $post = null ): string|false
- Since
- 2.1.0
- Source
wp-includes/post.php:1595
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.
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?
- Why does get_post_type() give the wrong result when I call it with no arguments?
- Can I pass a WP_Post object instead of an ID?
- Is it safe to use the return value directly in a database query or URL?
Why does get_post_type() return false instead of a string?
Why does get_post_type() give the wrong result when I call it with no arguments?
Can I pass a WP_Post object instead of an ID?
Is it safe to use the return value directly in a database query or URL?
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
- Scaling
- Constant
- Instructions
- 7–8
- Plugin surface
- None
- Called by
- 49
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 9.
Nothing here hands control to plugin code.
49 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–8 | get_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
- WP_Customize_Manager::find_changeset_post_id()Finds the changeset post ID for a given changeset UUID.
- WP_Customize_Manager::get_changeset_post_data()Gets the data stored in a changeset post.
- WP_Customize_Manager::grant_edit_post_capability_for_changeset()Re-maps 'edit_post' meta cap for a customize_changeset post to be the same as 'customize' maps.
- WP_Customize_Manager::has_published_pages()Returns whether there are published pages.
- WP_Customize_Manager::preserve_insert_changeset_post_content()Preserves the initial JSON post_content passed to save into the post.
- WP_Customize_Nav_Menus::save_nav_menus_created_posts()Publishes the auto-draft posts that were created for nav menu items.
- WP_Embed::find_oembed_post_id()Finds the oEmbed cache post ID for a given cache key.
- WP_REST_Attachments_Controller::create_item()Creates a single attachment.
- WP_REST_Attachments_Controller::handle_featured_media()Determines the featured media based on a request param.
- WP_REST_Attachments_Controller::update_item()Updates a single attachment.
- WP_REST_Menu_Items_Controller::prepare_item_for_database()Prepares a single nav menu item for create or update.
- WP_REST_Menu_Items_Controller::prepare_links()Prepares links for the request.
Show all 49
- WP_REST_Posts_Controller::prepare_item_for_database()Prepares a single post for create or update.
- WP_Theme::get_page_templates()Returns the theme's post templates for a given post type.
- WP_Widget_Media::has_content()Whether the widget has content to show.
- WP_Widget_Media_Gallery::has_content()Whether the widget has content to show.
- _disable_content_editor_for_navigation_post_type()This callback disables the content editor for wp_navigation type posts.
- _enable_content_editor_for_navigation_post_type()This callback enables content editor for wp_navigation type posts.
- _update_posts_count_on_transition_post_status()Handler for updating the current site's posts count when a post status changes.
- _wp_ajax_menu_quick_search()Prints the appropriate response to a menu quick search.
- block_core_calendar_update_has_published_post_on_transition_post_status()Handler for updating the has published posts flag when a post status changes.
- bulk_edit_posts()Processes the post data for the bulk editing of posts.
- get_attachment_link()Retrieves the permalink for an attachment.
- get_media_item()Retrieves HTML form for modifying the image attachment.
- get_object_subtype()Returns the object subtype for a given object ID of a specific type.
- get_oembed_response_data_rich()Filters the oEmbed response data to return an iframe embed code.
- get_the_category_list()Retrieves category list for a post in either HTML list or custom format.
- is_nav_menu_item()Determines whether the given ID is a nav menu item.
- is_post_publicly_viewable()Determines whether a post is publicly viewable.
- render_block_core_post_template()Renders the `core/post-template` block on the server.
- render_block_core_query_title()Renders the `core/query-title` block on the server.
- render_block_core_video()Renders the `core/video` block on the server to supply the width and height attributes from the attachment metadata.
- twenty_twenty_one_entry_meta_footer()Prints HTML with meta information for the categories, tags and comments.
- twenty_twenty_one_posted_by()Prints HTML with meta information about theme author.
- twentyfifteen_entry_meta()Prints HTML with meta information for the categories, tags.
- twentynineteen_entry_footer()Prints HTML with meta information for the categories, tags and comments.
- twentyseventeen_entry_footer()Prints HTML with meta information for the categories, tags and comments.
- twentysixteen_content_image_sizes_attr()Adds custom image sizes attribute to enhance responsive image functionality for content images
- twentysixteen_entry_meta()Prints HTML with meta information for the categories, tags.
- twentyten_posted_in()Prints HTML with meta information for the current post (category, tags and permalink).
- twentythirteen_entry_meta()Prints HTML with meta information for current post: categories, tags, permalink, author, and date.
- twentytwenty_get_post_meta()Retrieves the post meta.
- wp_autosave_post_revisioned_meta_fields()Autosaves the revisioned meta fields.
- wp_force_plain_post_permalink()Determine whether post should always use a plain permalink structure.
- wp_get_post_content_block_attributes()Retrieves Post Content block attributes from the current post template.
- wp_restore_post_revision_meta()Restore the revisioned meta values for a post.
- wp_save_revisioned_meta_fields()Save the revisioned meta fields.
- wp_set_post_categories()Sets categories for a post.
- wp_xmlrpc_server::_insert_post()Helper method for wp_newPost() and wp_editPost(), containing shared logic.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.