get_post_format( int|WP_Post|null $post = null ): string|false
- Since
- 3.1.0
- Source
wp-includes/post-formats.php:17
Compatibility
- WordPress
- since 3.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. Defaults to the current post in the loop.Default:
null
Return value
string|false- The format if successful. False otherwise.
Performance profile
How much work a call to get_post_format() 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–30
- Plugin surface
- None
- Called by
- 16
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 33.
Nothing here hands control to plugin code.
16 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 - cacheobject cache
wp_cache_add()one call below get_post_format() - hookthird-party callbacks
apply_filters()one call below get_post_format()
Further down the call graph this can also reach option, 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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_post_format() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7 | get_post() |
!post_type_supports() | 14 | get_post(), post_type_supports() |
post_type_supports() && empty($_format) | 23 | get_post(), post_type_supports(), get_the_terms() |
post_type_supports() && !empty($_format) | 30 | get_post(), post_type_supports(), get_the_terms(), reset() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 33 | 7–30 | 3 | |
| 8.5 | 33 | 7–30 | 3 | |
| 8.4 | 33 | 7–30 | 3 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 36 | 7–33 | 3 | |
| 8.2 | 36 | 7–33 | 3 | |
| 8.1 | 36 | 7–33 | 3 | |
| 7.4 | 36 | 7–33 | 3 |
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 · 3
- get_post()Retrieves post data given a post ID or post object.
- post_type_supports()Checks a post type's support for a given feature.
- get_the_terms()Retrieves the terms of the taxonomy that are attached to the post.
Used by · 16
- Twenty_Eleven_Ephemera_Widget::widget()Outputs the HTML for this widget.
- WP_REST_Posts_Controller::prepare_item_for_response()Prepares a single post output for response.
- _WP_Editors::editor_settings()
- get_body_class()Retrieves an array of the class names for the body element.
- get_embed_template()Retrieves an embed template path in the current or parent template.
- get_inline_data()Adds hidden fields with the data for use in the inline editor for posts and pages.
- get_post_class()Retrieves an array of the class names for the post container element.
- post_format_meta_box()Displays post format form elements.
- twenty_twenty_one_entry_meta_footer()Prints HTML with meta information for the categories, tags and comments.
- twentyfifteen_entry_meta()Prints HTML with meta information for the categories, tags.
- twentysixteen_entry_meta()Prints HTML with meta information for the categories, tags.
- twentythirteen_entry_date()Prints HTML with date information for current post.
Show all 16
- twentytwentyfive_format_binding()Callback function for the post format name block binding source.
- wp_xmlrpc_server::_prepare_post()Prepares post data for return in an XML-RPC object.
- wp_xmlrpc_server::mw_getPost()Retrieves a post.
- wp_xmlrpc_server::mw_getRecentPosts()Retrieves list of recent posts.
Source code
function get_post_format( $post = null ) { $post = get_post( $post ); if ( ! $post ) { return false; } if ( ! post_type_supports( $post->post_type, 'post-formats' ) ) { return false; } $_format = get_the_terms( $post->ID, 'post_format' ); if ( empty( $_format ) ) { return false; } $format = reset( $_format ); return str_replace( 'post-format-', '', $format->slug );}Changelog
Introduced in 3.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-formats.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.