> WordPress中文手册 > WordPress修改改文章状态:wp_publish_post()

【描述】
通过更改文章状态来发表文章。
【使用方法】

  <?PHP wp_publish_post( $post_id ) ?>

【参数】
$post_id

(整数)(必需)文章编号

默认值:None

返回的值
(空)

【示例】
注释
用法:$wpdb
用法:通过do_action() 调用一下函数 $post_id和$post(文章相关数据):
edit_post()
save_post()
wp_insert_post()
【修改记录】
自2.1.0版本后

【源文件】
wp_publish_post()位于wp-includes/post.php中。

/**
 * Publish a post by transitioning the post status.
 *
 * @since 2.1.0
 * @uses $wpdb
 * @uses do_action() Calls 'edit_post', 'save_post', and 'wp_insert_post' on post_id and post data.
 *
 * @param int $post_id Post ID.
 * @return null
 */
function wp_publish_post($post_id) {
 global $wpdb;

 $post = get_post($post_id);

 if ( empty($post) )
  return;

 if ( 'publish' == $post->post_status )
  return;

 $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post_id ) );

 $old_status = $post->post_status;
 $post->post_status = 'publish';
 wp_transition_post_status('publish', $old_status, $post);

 // Update counts for the post's terms.
 foreach ( (array) get_object_taxonomies('post') as $taxonomy ) {
  $tt_ids = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'tt_ids'));
  wp_update_term_count($tt_ids, $taxonomy);
 }

 do_action('edit_post', $post_id, $post);
 do_action('save_post', $post_id, $post);
 do_action('wp_insert_post', $post_id, $post);
}