WordPress修改改文章状态:wp_publish_post()


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

  1. <?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中。

  1. /**
  2. * Publish a post by transitioning the post status.
  3. *
  4. * @since 2.1.0
  5. * @uses $wpdb
  6. * @uses do_action() Calls 'edit_post', 'save_post', and 'wp_insert_post' on post_id and post data.
  7. *
  8. * @param int $post_id Post ID.
  9. * @return null
  10. */
  11. function wp_publish_post($post_id) {
  12. global $wpdb;
  13.  
  14. $post = get_post($post_id);
  15.  
  16. if ( empty($post) )
  17. return;
  18.  
  19. if ( 'publish' == $post->post_status )
  20. return;
  21.  
  22. $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post_id ) );
  23.  
  24. $old_status = $post->post_status;
  25. $post->post_status = 'publish';
  26. wp_transition_post_status('publish', $old_status, $post);
  27.  
  28. // Update counts for the post's terms.
  29. foreach ( (array) get_object_taxonomies('post') as $taxonomy ) {
  30. $tt_ids = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'tt_ids'));
  31. wp_update_term_count($tt_ids, $taxonomy);
  32. }
  33.  
  34. do_action('edit_post', $post_id, $post);
  35. do_action('save_post', $post_id, $post);
  36. do_action('wp_insert_post', $post_id, $post);
  37. }