post_type_supports( string $post_type, string $feature ): bool
- Since
- 3.0.0
- Source
wp-includes/post.php:2381
Looks up whether a post type declares support for one feature, such as 'thumbnail' or 'comments', and returns a plain boolean. It reads the internal $_wp_post_type_features array that add_post_type_support() fills in, so the result depends on when in the request that support was registered. A false result also comes back for a post type that isn't registered at all, so it can't be used on its own to confirm the post type exists.
Compatibility
- WordPress
- since 3.0.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 post type being checked.
$featurestring- The feature being checked.
Return value
bool- Whether the post type supports the given feature.
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 which features a post type supports
Loop over a handful of common feature names and print whether the built-in post and page post types support each one.
$features = array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'page-attributes' );
foreach ( array( 'post', 'page' ) as $post_type ) {
foreach ( $features as $feature ) {
printf(
'%s supports %s: %s<br>',
esc_html( $post_type ),
esc_html( $feature ),
post_type_supports( $post_type, $feature ) ? 'yes' : 'no'
);
}
}Show an admin notice only on post types that allow comments
Register an admin_notices callback that inspects the current screen's post type before deciding what to display.
add_action( 'admin_notices', function () {
$screen = get_current_screen();
if ( ! $screen || empty( $screen->post_type ) ) {
return;
}
if ( post_type_supports( $screen->post_type, 'comments' ) ) {
echo '<div class="notice notice-info"><p>' . esc_html( $screen->post_type ) . ' supports comments, so the Discussion panel is available here.</p></div>';
} else {
echo '<div class="notice notice-warning"><p>' . esc_html( $screen->post_type ) . ' does not support comments.</p></div>';
}
} );Open an edit screen for a post or page in wp-admin to see the notice fire.
Common problems and fixes · 3
- Why does post_type_supports() return false right after I called add_post_type_support()?
- Why does post_type_supports() return true but the meta box I expect still isn't there?
- Can I use post_type_supports() to check if a custom post type is registered?
Why does post_type_supports() return false right after I called add_post_type_support()?
Why does post_type_supports() return true but the meta box I expect still isn't there?
Can I use post_type_supports() to check if a custom post type is registered?
Alternatives and related functions
add_post_type_support- When you need to register a feature for a post type rather than just read whether one is already registered.
get_all_post_type_supports- When you need the full list of features a post type supports instead of testing one feature at a time.
post_type_exists- When you need to confirm a post type is registered at all, since a false result here doesn't distinguish that case.
current_theme_supports- When the capability you're checking belongs to the active theme, such as post thumbnails or custom logos, rather than to a post type's own declared features.
Performance profile
How much work a call to post_type_supports() 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
- Scaling
- Constant
- Instructions
- 6
- Plugin surface
- None
- Called by
- 45
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 6.
Nothing here hands control to plugin code.
45 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 post_type_supports() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | none |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 6 instructions, 6 executed per call, 0 branches. 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.
Used by · 45
- WP_Media_List_Table::get_columns()
- WP_Post_Type::add_supports()Sets the features support for the post type.
- WP_Post_Type::get_autosave_rest_controller()Gets the REST API autosave controller for this post type.
- WP_Post_Type::get_revisions_rest_controller()Gets the REST API revisions controller for this post type.
- WP_Posts_List_Table::_display_rows()
- WP_Posts_List_Table::get_columns()
- WP_Posts_List_Table::inline_edit()Outputs the hidden row displayed when inline editing
- WP_REST_Attachments_Controller::handle_featured_media()Determines the featured media based on a request param.
- WP_REST_Global_Styles_Controller::prepare_links()Prepares links for the request.
- WP_REST_Post_Search_Handler::prepare_item()Prepares the search result for a given post ID.
- WP_REST_Posts_Controller::get_available_actions()Gets the link relations available for the post and current user.
- WP_REST_Posts_Controller::get_collection_params()Retrieves the query params for the posts collection.
Show all 45
- WP_REST_Posts_Controller::get_item_schema()Retrieves the post's schema, conforming to JSON Schema.
- WP_REST_Posts_Controller::get_items()Retrieves a collection of posts.
- WP_REST_Posts_Controller::get_schema_links()Retrieves Link Description Objects that should be added to the Schema for the posts collection.
- WP_REST_Posts_Controller::prepare_links()Prepares links for the request.
- WP_REST_Templates_Controller::prepare_links()Prepares links for the request.
- WP_REST_Users_Controller::get_items_permissions_check()Permissions check for getting all users.
- _WP_Editors::editor_settings()
- edit_post()Updates an existing post with values provided in `$_POST`.
- get_body_class()Retrieves an array of the class names for the body element.
- get_default_comment_status()Gets the default comment status for a post type.
- get_default_post_to_edit()Returns default post information to use when populating the "Write Post" form.
- get_inline_data()Adds hidden fields with the data for use in the inline editor for posts and pages.
- get_media_item()Retrieves HTML form for modifying the image attachment.
- get_post_class()Retrieves an array of the class names for the post container element.
- get_post_format()Retrieve the format slug for a post
- page_attributes_meta_box()Displays page attributes form fields.
- post_format_meta_box()Displays post format form elements.
- register_and_do_post_meta_boxes()Registers the default post meta boxes, and runs the `do_meta_boxes` actions.
- register_block_core_footnotes_post_meta()Registers the footnotes meta field required for footnotes to work.
- register_meta()Registers a meta key.
- render_block_core_post_author()Renders the `core/post-author` block on the server.
- render_block_core_post_author_name()Renders the `core/post-author-name` block on the server.
- twenty_twenty_one_posted_by()Prints HTML with meta information about theme author.
- twentytwenty_get_post_meta()Retrieves the post meta.
- use_block_editor_for_post_type()Returns whether a post type is compatible with the block editor.
- wp_delete_user()Delete user and optionally reassign posts and links to another user.
- wp_enqueue_media()Enqueues all scripts, styles, settings, and templates necessary to use all media JS APIs.
- wp_generate_attachment_metadata()Generates attachment meta data and create image sub-sizes for images.
- wp_insert_post()Inserts or updates a post in the database.
- wp_print_media_templates()Prints the templates used in the media manager.
- wp_revisions_to_keep()Determines how many revisions to retain for a given post.
- wp_save_post_revision()Creates a revision for the current version of a post.
- wp_xmlrpc_server::wp_getComments()Retrieves comments.
Source code
function post_type_supports( $post_type, $feature ) { global $_wp_post_type_features; return ( isset( $_wp_post_type_features[ $post_type ][ $feature ] ) );}Changelog
Introduced in 3.0.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 7.1.0 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.