wp_publish_post

The timeline below displays how wordpress function wp_publish_post has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Publishes a post by transitioning the post status.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Post $post Post ID or post object.
 */
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);
}

WordPress Version: 5.6

/**
 * Publish a post by transitioning the post status.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Post $post Post ID or post object.
 */
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);
}

WordPress Version: 5.5

/**
 * Publish a post by transitioning the post status.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Post $post Post ID or post object.
 */
function wp_publish_post($post)
{
    global $wpdb;
    $post = get_post($post);
    if (!$post) {
        return;
    }
    if ('publish' === $post->post_status) {
        return;
    }
    $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);
}

WordPress Version: 5.3

/**
 * Publish a post by transitioning the post status.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Post $post Post ID or post object.
 */
function wp_publish_post($post)
{
    global $wpdb;
    $post = get_post($post);
    if (!$post) {
        return;
    }
    if ('publish' == $post->post_status) {
        return;
    }
    $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);
}

WordPress Version: 5.1

/**
 * Publish a post by transitioning the post status.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Post $post Post ID or post object.
 */
function wp_publish_post($post)
{
    global $wpdb;
    if (!$post = get_post($post)) {
        return;
    }
    if ('publish' == $post->post_status) {
        return;
    }
    $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);
}

WordPress Version: 4.0

/**
 * Publish a post by transitioning the post status.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Post $post Post ID or post object.
 */
function wp_publish_post($post)
{
    global $wpdb;
    if (!$post = get_post($post)) {
        return;
    }
    if ('publish' == $post->post_status) {
        return;
    }
    $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->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);
}

WordPress Version: 3.9

/**
 * Publish a post by transitioning the post status.
 *
 * @since 2.1.0
 * @uses $wpdb
 *
 * @param int|WP_Post $post Post ID or post object.
 */
function wp_publish_post($post)
{
    global $wpdb;
    if (!$post = get_post($post)) {
        return;
    }
    if ('publish' == $post->post_status) {
        return;
    }
    $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->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);
}

WordPress Version: 3.7

/**
 * Publish a post by transitioning the post status.
 *
 * @since 2.1.0
 * @uses $wpdb
 * @uses do_action() Calls 'edit_post', 'save_post_{$post_type}', 'save_post' and 'wp_insert_post' on post_id and post data.
 *
 * @param int|object $post Post ID or object.
 */
function wp_publish_post($post)
{
    global $wpdb;
    if (!$post = get_post($post)) {
        return;
    }
    if ('publish' == $post->post_status) {
        return;
    }
    $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);
    do_action('edit_post', $post->ID, $post);
    do_action("save_post_{$post->post_type}", $post->ID, $post, true);
    do_action('save_post', $post->ID, $post, true);
    do_action('wp_insert_post', $post->ID, $post, true);
}