函数文档

wp_publish_post()

💡 云策文档标注

概述

wp_publish_post() 函数用于通过转换文章状态来发布文章。它主要处理状态更新、默认分类设置,并触发相关钩子,但不设置 post_name,建议使用 wp_update_post() 进行更全面的更新。

关键要点

  • 函数接受文章 ID 或 WP_Post 对象作为参数,将文章状态转换为 'publish'。
  • 如果文章已发布或不存在,函数会提前返回,不执行任何操作。
  • 自动为没有设置分类的文章应用默认分类(如默认分类或其他分类法的默认项)。
  • 更新数据库中的文章状态,清理缓存,并触发 wp_transition_post_status() 和相关动作钩子(如 edit_post、save_post)。
  • 注意:此函数仅处理状态转换,不设置 post_name,需使用 wp_update_post() 确保完整更新。

代码示例

// 示例:发布一个文章
$post_id = 123;
wp_publish_post($post_id);

注意事项

开发者应注意,此函数功能有限,主要用于内部或特定场景(如发布定时文章),常规发布操作可能需结合其他函数以确保数据完整性。


📄 原文内容

Publishes a post by transitioning the post status.

Parameters

$postint|WP_Postrequired
Post ID or post object.

More Information

This function does not do anything except transition the post status. If you want to ensure post_name is set, use wp_update_post() instead.

Source

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

Hooks

do_action( ‘edit_post’, int $post_id, WP_Post $post )

Fires once an existing post has been updated.

do_action( “edit_post_{$post->post_type}”, int $post_id, WP_Post $post )

Fires once an existing post has been updated.

do_action( ‘save_post’, int $post_id, WP_Post $post, bool $update )

Fires once a post has been saved.

do_action( “save_post_{$post->post_type}”, int $post_id, WP_Post $post, bool $update )

Fires once a post has been saved.

do_action( ‘wp_insert_post’, int $post_id, WP_Post $post, bool $update )

Fires once a post has been saved.

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Feedback: The function name does not seem to indicate that it does not do the usual actions expected from transitioning a post to publish status. The note inside “more information” is easy to miss.

    Can the following note be moved from more information to the main description?
    This function does not do anything except transition the post status. If you want to ensure post_name is set, use wp_update_post() instead.

    P.S. Just a thought (I don’t have the full context), if the function is only supposed to be used when publishing a future post then consider deprecating this and creating a new one called `wp_publish_future_post`?