wp_publish_post()WP 2.1.0

Publish a post by transitioning the post status.

Return

null. Nothing (null).

Usage

wp_publish_post( $post );
$post(int|WP_Post) (required)
Post ID or post object.

Examples

0

#1 Delay post Publication

Suppose we need to publish a post only if some check is passed. To do this, we add the post to the database with a status of pending. Then we do our test and if it passed we publish the post, but if it didn't pass we do nothing.

function add_coin_post(){

	//Add the post to the database

	$post_data = [
		'post_type'   => 'coin',
		'post_title'  => $args['name'],
		'post_name'   => sanitize_title( $symbol ),
		'post_status' => 'pending', // ! IMPORTANT
	];

	$post_id = wp_insert_post( wp_slash( $post_data ), true, false );

	if( is_wp_error( $post_id ) ){
		return $post_id;
	}

	// DO THE CHECK WE NEED
	// where the variable $check_is_ok will be defined, if everything is ok

	if( $check_is_ok ){

		// run the publication hooks on which the
		// operations for a new published post
		wp_publish_post( $post_id );
	}

}

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 2.1.0 Introduced.

wp_publish_post() code WP 6.5.2

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 );
}