wp_publish_post( int|WP_Post $post )
- Since
- 2.1.0
- Source
wp-includes/post.php:5404
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- Post ID or post object.
Performance profile
How much work a call to wp_publish_post() 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
- Scales with input
- Instructions
- 8–95
- Plugin surface
- 5 hooks
- Called by
- 1
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 131.
Third-party callbacks on 'edit_post_{$post->post_type}', 'edit_post', 'save_post_{$post->post_type}' run inside this call, and their cost is not bounded by anything here.
1 place 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 - optionoption read or write
get_option()called directly - hookthird-party callbacks
do_action()called directly - cacheobject cache
wp_cache_add()one call below wp_publish_post() - serializeserialisation
maybe_unserialize()one call below wp_publish_post()
Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_publish_post() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8–11 | get_post() |
| always | 94–95 | get_post(), get_post(), get_object_taxonomies(), ID(), clean_post_cache(), wp_transition_post_status(), do_action(), do_action(), do_action(), do_action(), do_action(), wp_after_insert_post() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 131 | 8–95 | 9 | |
| 8.5 | 131 | 8–95 | 9 | |
| 8.4 | 131 | 8–95 | 9 | |
| 8.3 | 131 | 8–95 | 9 | |
| 8.2 | 131 | 8–95 | 9 | Different branch profile from PHP 8.1 |
| 8.1 | 131 | 8–95 | 10 | |
| 7.4 | 131 | 8–95 | 10 |
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.
Hooks and filters fired · 5
5 hooks fire while wp_publish_post() runs, in this order:
- do_action( edit_post_{$post->post_type} )actionline 5455 (+51 into the body)
Fires once an existing post has been updated.
- do_action( edit_post )actionline 5458 (+54 into the body)
Fires once an existing post has been updated.
- do_action( save_post_{$post->post_type} )actionline 5461 (+57 into the body)
Fires once a post has been saved.
Uses · 9
- get_post()Retrieves post data given a post ID or post object.
- get_object_taxonomies()Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.
- get_the_terms()Retrieves the terms of the taxonomy that are attached to the post.
- get_option()Retrieves an option value based on an option name.
- wp_set_post_terms()Sets the terms for a post.
- clean_post_cache()Will clean the post in the cache.
- wp_transition_post_status()Fires actions related to the transitioning of a post's status.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_after_insert_post()Fires actions after a post, its terms and meta data has been saved.
Used by · 1
- check_and_publish_future_post()Publishes future post and make sure post ID has future post status.
Source code
function wp_publish_post( $post ) { global $wpdb; $post = get_post( $post ); if ( ! $post ) { return; } if ( 'publish' === $post->post_status ) { return; } $post_before = get_post( $post->ID ); // Ensure at least one term is applied for taxonomies with a default term. foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) { // Skip taxonomy if no default term is set. if ( 'category' !== $taxonomy && empty( $tax_object->default_term ) ) { continue; } // Do not modify previously set terms. if ( ! empty( get_the_terms( $post, $taxonomy ) ) ) { continue; } if ( 'category' === $taxonomy ) { $default_term_id = (int) get_option( 'default_category', 0 ); } else { $default_term_id = (int) get_option( 'default_term_' . $taxonomy, 0 ); } if ( ! $default_term_id ) { continue; } wp_set_post_terms( $post->ID, array( $default_term_id ), $taxonomy ); } $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) ); clean_post_cache( $post->ID ); $old_status = $post->post_status; $post->post_status = 'publish'; wp_transition_post_status( 'publish', $old_status, $post ); /** This action is documented in wp-includes/post.php */ do_action( "edit_post_{$post->post_type}", $post->ID, $post ); /** This action is documented in wp-includes/post.php */ do_action( 'edit_post', $post->ID, $post ); /** This action is documented in wp-includes/post.php */ do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); /** This action is documented in wp-includes/post.php */ do_action( 'save_post', $post->ID, $post, true ); /** This action is documented in wp-includes/post.php */ do_action( 'wp_insert_post', $post->ID, $post, true ); wp_after_insert_post( $post, true, $post_before );}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 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.