set_post_format( int|WP_Post $post, string $format ): array|WP_Error|false
- Since
- 3.1.0
- Source
wp-includes/post-formats.php:70
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- The post for which to assign a format.
$formatstring- A format to assign. Use an empty string or array to remove all formats from the post.
Return value
array|WP_Error|false- Array of affected term IDs on success. WP_Error on error.
Performance profile
How much work a call to set_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
- 15–30
- Plugin surface
- None
- Called by
- 8
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 40.
Nothing here hands control to plugin code.
8 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 - hookthird-party callbacks
apply_filters()one call below set_post_format()
Further down the call graph this can also reach cache, 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 set_post_format() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15 | get_post(), __() |
empty($format) | 17 | get_post(), wp_set_post_terms() |
!empty($format) && $format === "standard" | 25 | get_post(), sanitize_key(), wp_set_post_terms() |
!empty($format) && $format !== "standard" | 30 | get_post(), sanitize_key(), get_post_format_slugs(), wp_set_post_terms() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 40 | 15–30 | 4 | |
| 8.5 | 40 | 15–30 | 4 | |
| 8.4 | 40 | 15–30 | 4 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 43 | 15–33 | 4 | |
| 8.2 | 43 | 15–33 | 4 | |
| 8.1 | 43 | 15–33 | 4 | |
| 7.4 | 43 | 15–33 | 4 |
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 · 6
- get_post()Retrieves post data given a post ID or post object.
- __()Retrieves the translation of $text.
- sanitize_key()Sanitizes a string key.
- get_post_format_slugs()Retrieves the array of post format slugs.
- wp_set_post_terms()Sets the terms for a post.
- WP_Error::__construct()Initializes the error.
Used by · 8
- WP_REST_Posts_Controller::create_item()Creates a single post.
- WP_REST_Posts_Controller::update_item()Updates a single post.
- bulk_edit_posts()Processes the post data for the bulk editing of posts.
- edit_post()Updates an existing post with values provided in `$_POST`.
- get_default_post_to_edit()Returns default post information to use when populating the "Write Post" form.
- wp_xmlrpc_server::_insert_post()Helper method for wp_newPost() and wp_editPost(), containing shared logic.
- wp_xmlrpc_server::mw_editPost()Edits a post.
- wp_xmlrpc_server::mw_newPost()Creates a new post.
Source code
function set_post_format( $post, $format ) { $post = get_post( $post ); if ( ! $post ) { return new WP_Error( 'invalid_post', __( 'Invalid post.' ) ); } if ( ! empty( $format ) ) { $format = sanitize_key( $format ); if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs(), true ) ) { $format = ''; } else { $format = 'post-format-' . $format; } } return wp_set_post_terms( $post->ID, $format, 'post_format' );}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 7.0.4 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.