Retrieves the post type of the current post or of a given post.
Parameters
$postint|WP_Post|nulloptional- Post ID or post object. Default is global $post.
Default:
null
Return
string|false Post type on success, false on failure.Source
function get_post_type( $post = null ) {
$post = get_post( $post );
if ( $post ) {
return $post->post_type;
}
return false;
}
Related
| Uses | Description |
|---|---|
get_post()wp-includes/post.php | Retrieves post data given a post ID or post object. |
| Used by | Description |
|---|---|
WP_REST_Attachments_Controller::handle_featured_media()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | Determines the featured media based on a request param. |
wp_restore_post_revision_meta()wp-includes/revision.php | Restore the revisioned meta values for a post. |
wp_save_revisioned_meta_fields()wp-includes/revision.php | Save the revisioned meta fields. |
wp_autosave_post_revisioned_meta_fields()wp-admin/includes/post.php | Autosaves the revisioned meta fields. |
wp_get_post_content_block_attributes()wp-includes/block-editor.php | Retrieves Post Content block attributes from the current post template. |
WP_REST_Menu_Items_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php | Prepares links for the request. |
WP_REST_Menu_Items_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php | Prepares a single nav menu item for create or update. |
_disable_content_editor_for_navigation_post_type()wp-admin/includes/post.php | This callback disables the content editor for wp_navigation type posts. |
_enable_content_editor_for_navigation_post_type()wp-admin/includes/post.php | This callback enables content editor for wp_navigation type posts. |
wp_force_plain_post_permalink()wp-includes/link-template.php | Determine whether post should always use a plain permalink structure. |
is_post_publicly_viewable()wp-includes/post.php | Determines whether a post is publicly viewable. |
WP_Customize_Manager::preserve_insert_changeset_post_content()wp-includes/class-wp-customize-manager.php | Preserves the initial JSON post_content passed to save into the post. |
get_object_subtype()wp-includes/meta.php | Returns the object subtype for a given object ID of a specific type. |
WP_Customize_Manager::grant_edit_post_capability_for_changeset()wp-includes/class-wp-customize-manager.php | Re-maps ‘edit_post’ meta cap for a customize_changeset post to be the same as ‘customize’ maps. |
WP_Embed::find_oembed_post_id()wp-includes/class-wp-embed.php | Finds the oEmbed cache post ID for a given cache key. |
WP_Widget_Media_Gallery::has_content()wp-includes/widgets/class-wp-widget-media-gallery.php | Whether the widget has content to show. |
WP_Widget_Media::has_content()wp-includes/widgets/class-wp-widget-media.php | Whether the widget has content to show. |
WP_Customize_Manager::has_published_pages()wp-includes/class-wp-customize-manager.php | Returns whether there are published pages. |
WP_Customize_Manager::find_changeset_post_id()wp-includes/class-wp-customize-manager.php | Finds the changeset post ID for a given changeset UUID. |
WP_Customize_Manager::get_changeset_post_data()wp-includes/class-wp-customize-manager.php | Gets the data stored in a changeset post. |
WP_REST_Attachments_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | Creates a single attachment. |
WP_REST_Attachments_Controller::update_item()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | Updates a single attachment. |
WP_REST_Posts_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Prepares a single post for create or update. |
WP_Customize_Nav_Menus::save_nav_menus_created_posts()wp-includes/class-wp-customize-nav-menus.php | Publishes the auto-draft posts that were created for nav menu items. |
get_oembed_response_data_rich()wp-includes/embed.php | Filters the oEmbed response data to return an iframe embed code. |
_update_posts_count_on_transition_post_status()wp-includes/ms-blogs.php | Handler for updating the current site’s posts count when a post status changes. |
get_media_item()wp-admin/includes/media.php | Retrieves HTML form for modifying the image attachment. |
bulk_edit_posts()wp-admin/includes/post.php | Processes the post data for the bulk editing of posts. |
_wp_ajax_menu_quick_search()wp-admin/includes/nav-menu.php | Prints the appropriate response to a menu quick search. |
get_the_category_list()wp-includes/category-template.php | Retrieves category list for a post in either HTML list or custom format. |
WP_Theme::get_page_templates()wp-includes/class-wp-theme.php | Returns the theme’s post templates for a given post type. |
get_attachment_link()wp-includes/link-template.php | Retrieves the permalink for an attachment. |
wp_set_post_categories()wp-includes/post.php | Sets categories for a post. |
is_nav_menu_item()wp-includes/nav-menu.php | Determines whether the given ID is a nav menu item. |
wp_xmlrpc_server::_insert_post()wp-includes/class-wp-xmlrpc-server.php | Helper method for wp_newPost() and wp_editPost(), containing shared logic. |
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |
Default WordPress Post Type Names
Here are the default post type names. Use these as the parameter for this function to collect the post type object
postpageattachmentrevisionnav_menu_itemConditional sentence to see if it is a post type.
get_post_type()andget_the_ID()use the global $post so you don’t need to useget_the_ID()here. You can do with onlyget_post_type() === 'slug_post_type'.Display the post type. This example needs to be inside the loop.
In some cases, such as when you’re outside The Loop, you may need to pass get_queried_object_id() as a parameter to get the post type of the current queried object:
Returns true if the given post ID is associated with a portfolio or product
function check_post_in_portfolio_or_product( $post_id ) {
$post_type = get_post_type( $post_id );
if ( in_array( $post_type, array( 'portfolio', 'product' ), true ) ) {
return true;
}
return false;
}
Example:
$post_id = 123;
if ( check_post_in_portfolio_or_product( $post_id ) ) {
// Do something only if it's a portfolio or product post.
}
get_post_type();is commonly used in conjunction with Post Formats.
Functionality is extended in themes by including:
add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );in your functions.php file.
After including this in your theme’s functions.php file, the option to choose a post type (that you included inside the array) will appear in the right sidebar when creating/editing a post.
If no Post Type is selected, WordPress selects the default which is
Standard.WordPress supported post formats (that are not defaults) are:
'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat'Everything you need to know about using & styling Post Formats is here: https://codex.wordpress.org/Post_Formats