wppaste
WordPress

wp_publish_post( int|WP_Post $post )

Since
2.1.0
Source
wp-includes/post.php:5404
Publishes a post by transitioning the post status.

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

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
8–95

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 131.

Plugin surface
5 hooks

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.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksdo_action()called directly
  • cacheobject cachewp_cache_add()one call below wp_publish_post()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
always8–11get_post()
always94–95get_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

PHPCompiledExecutedBranchesNotes
8.6-dev1318–959
8.51318–959
8.41318–959
8.31318–959
8.21318–959Different branch profile from PHP 8.1
8.11318–9510
7.41318–9510

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:

  1. do_action( edit_post_{$post->post_type} )actionline 5455 (+51 into the body)

    Fires once an existing post has been updated.

  2. do_action( edit_post )actionline 5458 (+54 into the body)

    Fires once an existing post has been updated.

  3. do_action( save_post_{$post->post_type} )actionline 5461 (+57 into the body)

    Fires once a post has been saved.

  4. do_action( save_post )actionline 5464 (+60 into the body)

    Fires once a post has been saved.

  5. do_action( wp_insert_post )actionline 5467 (+63 into the body)

    Fires once a post has been saved.

Uses · 9

Used by · 1

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.