wp_set_comment_status

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

WordPress Version: 5.9

/**
 * Sets the status of a comment.
 *
 * The {@see 'wp_set_comment_status'} action is called after the comment is handled.
 * If the comment status is not in the list, then false is returned.
 *
 * @since 1.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Comment $comment_id     Comment ID or WP_Comment object.
 * @param string         $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
 * @param bool           $wp_error       Whether to return a WP_Error object if there is a failure. Default false.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
{
    global $wpdb;
    switch ($comment_status) {
        case 'hold':
        case '0':
            $status = '0';
            break;
        case 'approve':
        case '1':
            $status = '1';
            add_action('wp_set_comment_status', 'wp_new_comment_notify_postauthor');
            break;
        case 'spam':
            $status = 'spam';
            break;
        case 'trash':
            $status = 'trash';
            break;
        default:
            return false;
    }
    $comment_old = clone get_comment($comment_id);
    if (!$wpdb->update($wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_old->comment_ID))) {
        if ($wp_error) {
            return new WP_Error('db_update_error', __('Could not update comment status.'), $wpdb->last_error);
        } else {
            return false;
        }
    }
    clean_comment_cache($comment_old->comment_ID);
    $comment = get_comment($comment_old->comment_ID);
    /**
     * Fires immediately after transitioning a comment's status from one to another in the database
     * and removing the comment from the object cache, but prior to all status transition hooks.
     *
     * @since 1.5.0
     *
     * @param string $comment_id     Comment ID as a numeric string.
     * @param string $comment_status Current comment status. Possible values include
     *                               'hold', '0', 'approve', '1', 'spam', and 'trash'.
     */
    do_action('wp_set_comment_status', $comment->comment_ID, $comment_status);
    wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
    wp_update_comment_count($comment->comment_post_ID);
    return true;
}

WordPress Version: 5.7

/**
 * Sets the status of a comment.
 *
 * The {@see 'wp_set_comment_status'} action is called after the comment is handled.
 * If the comment status is not in the list, then false is returned.
 *
 * @since 1.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Comment $comment_id     Comment ID or WP_Comment object.
 * @param string         $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
 * @param bool           $wp_error       Whether to return a WP_Error object if there is a failure. Default false.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
{
    global $wpdb;
    switch ($comment_status) {
        case 'hold':
        case '0':
            $status = '0';
            break;
        case 'approve':
        case '1':
            $status = '1';
            add_action('wp_set_comment_status', 'wp_new_comment_notify_postauthor');
            break;
        case 'spam':
            $status = 'spam';
            break;
        case 'trash':
            $status = 'trash';
            break;
        default:
            return false;
    }
    $comment_old = clone get_comment($comment_id);
    if (!$wpdb->update($wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_old->comment_ID))) {
        if ($wp_error) {
            return new WP_Error('db_update_error', __('Could not update comment status.'), $wpdb->last_error);
        } else {
            return false;
        }
    }
    clean_comment_cache($comment_old->comment_ID);
    $comment = get_comment($comment_old->comment_ID);
    /**
     * Fires immediately after transitioning a comment's status from one to another in the database
     * and removing the comment from the object cache, but prior to all status transition hooks.
     *
     * @since 1.5.0
     *
     * @param int    $comment_id     Comment ID.
     * @param string $comment_status Current comment status. Possible values include
     *                               'hold', '0', 'approve', '1', 'spam', and 'trash'.
     */
    do_action('wp_set_comment_status', $comment->comment_ID, $comment_status);
    wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
    wp_update_comment_count($comment->comment_post_ID);
    return true;
}

WordPress Version: 5.6

/**
 * Sets the status of a comment.
 *
 * The {@see 'wp_set_comment_status'} action is called after the comment is handled.
 * If the comment status is not in the list, then false is returned.
 *
 * @since 1.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Comment $comment_id     Comment ID or WP_Comment object.
 * @param string         $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
 * @param bool           $wp_error       Whether to return a WP_Error object if there is a failure. Default false.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
{
    global $wpdb;
    switch ($comment_status) {
        case 'hold':
        case '0':
            $status = '0';
            break;
        case 'approve':
        case '1':
            $status = '1';
            add_action('wp_set_comment_status', 'wp_new_comment_notify_postauthor');
            break;
        case 'spam':
            $status = 'spam';
            break;
        case 'trash':
            $status = 'trash';
            break;
        default:
            return false;
    }
    $comment_old = clone get_comment($comment_id);
    if (!$wpdb->update($wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_old->comment_ID))) {
        if ($wp_error) {
            return new WP_Error('db_update_error', __('Could not update comment status.'), $wpdb->last_error);
        } else {
            return false;
        }
    }
    clean_comment_cache($comment_old->comment_ID);
    $comment = get_comment($comment_old->comment_ID);
    /**
     * Fires immediately after transitioning a comment's status from one to another in the database
     * and removing the comment from the object cache, but prior to all status transition hooks.
     *
     * @since 1.5.0
     *
     * @param int         $comment_id     Comment ID.
     * @param string|bool $comment_status Current comment status. Possible values include
     *                                    'hold', 'approve', 'spam', 'trash', or false.
     */
    do_action('wp_set_comment_status', $comment->comment_ID, $comment_status);
    wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
    wp_update_comment_count($comment->comment_post_ID);
    return true;
}

WordPress Version: 5.5

/**
 * Sets the status of a comment.
 *
 * The {@see 'wp_set_comment_status'} action is called after the comment is handled.
 * If the comment status is not in the list, then false is returned.
 *
 * @since 1.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Comment $comment_id     Comment ID or WP_Comment object.
 * @param string         $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
 * @param bool           $wp_error       Whether to return a WP_Error object if there is a failure. Default false.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
{
    global $wpdb;
    switch ($comment_status) {
        case 'hold':
        case '0':
            $status = '0';
            break;
        case 'approve':
        case '1':
            $status = '1';
            add_action('wp_set_comment_status', 'wp_new_comment_notify_postauthor');
            break;
        case 'spam':
            $status = 'spam';
            break;
        case 'trash':
            $status = 'trash';
            break;
        default:
            return false;
    }
    $comment_old = clone get_comment($comment_id);
    if (!$wpdb->update($wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_old->comment_ID))) {
        if ($wp_error) {
            return new WP_Error('db_update_error', __('Could not update comment status.'), $wpdb->last_error);
        } else {
            return false;
        }
    }
    clean_comment_cache($comment_old->comment_ID);
    $comment = get_comment($comment_old->comment_ID);
    /**
     * Fires immediately before transitioning a comment's status from one to another
     * in the database.
     *
     * @since 1.5.0
     *
     * @param int         $comment_id     Comment ID.
     * @param string|bool $comment_status Current comment status. Possible values include
     *                                    'hold', 'approve', 'spam', 'trash', or false.
     */
    do_action('wp_set_comment_status', $comment->comment_ID, $comment_status);
    wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
    wp_update_comment_count($comment->comment_post_ID);
    return true;
}

WordPress Version: 4.6

/**
 * Sets the status of a comment.
 *
 * The {@see 'wp_set_comment_status'} action is called after the comment is handled.
 * If the comment status is not in the list, then false is returned.
 *
 * @since 1.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Comment $comment_id     Comment ID or WP_Comment object.
 * @param string         $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
 * @param bool           $wp_error       Whether to return a WP_Error object if there is a failure. Default is false.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
{
    global $wpdb;
    switch ($comment_status) {
        case 'hold':
        case '0':
            $status = '0';
            break;
        case 'approve':
        case '1':
            $status = '1';
            add_action('wp_set_comment_status', 'wp_new_comment_notify_postauthor');
            break;
        case 'spam':
            $status = 'spam';
            break;
        case 'trash':
            $status = 'trash';
            break;
        default:
            return false;
    }
    $comment_old = clone get_comment($comment_id);
    if (!$wpdb->update($wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_old->comment_ID))) {
        if ($wp_error) {
            return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
        } else {
            return false;
        }
    }
    clean_comment_cache($comment_old->comment_ID);
    $comment = get_comment($comment_old->comment_ID);
    /**
     * Fires immediately before transitioning a comment's status from one to another
     * in the database.
     *
     * @since 1.5.0
     *
     * @param int         $comment_id     Comment ID.
     * @param string|bool $comment_status Current comment status. Possible values include
     *                                    'hold', 'approve', 'spam', 'trash', or false.
     */
    do_action('wp_set_comment_status', $comment->comment_ID, $comment_status);
    wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
    wp_update_comment_count($comment->comment_post_ID);
    return true;
}

WordPress Version: 4.4

/**
 * Sets the status of a comment.
 *
 * The 'wp_set_comment_status' action is called after the comment is handled.
 * If the comment status is not in the list, then false is returned.
 *
 * @since 1.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Comment $comment_id     Comment ID or WP_Comment object.
 * @param string         $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
 * @param bool           $wp_error       Whether to return a WP_Error object if there is a failure. Default is false.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
{
    global $wpdb;
    switch ($comment_status) {
        case 'hold':
        case '0':
            $status = '0';
            break;
        case 'approve':
        case '1':
            $status = '1';
            add_action('wp_set_comment_status', 'wp_new_comment_notify_postauthor');
            break;
        case 'spam':
            $status = 'spam';
            break;
        case 'trash':
            $status = 'trash';
            break;
        default:
            return false;
    }
    $comment_old = clone get_comment($comment_id);
    if (!$wpdb->update($wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_old->comment_ID))) {
        if ($wp_error) {
            return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
        } else {
            return false;
        }
    }
    clean_comment_cache($comment_old->comment_ID);
    $comment = get_comment($comment_old->comment_ID);
    /**
     * Fires immediately before transitioning a comment's status from one to another
     * in the database.
     *
     * @since 1.5.0
     *
     * @param int         $comment_id     Comment ID.
     * @param string|bool $comment_status Current comment status. Possible values include
     *                                    'hold', 'approve', 'spam', 'trash', or false.
     */
    do_action('wp_set_comment_status', $comment->comment_ID, $comment_status);
    wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
    wp_update_comment_count($comment->comment_post_ID);
    return true;
}

WordPress Version: 4.3

/**
 * Sets the status of a comment.
 *
 * The 'wp_set_comment_status' action is called after the comment is handled.
 * If the comment status is not in the list, then false is returned.
 *
 * @since 1.0.0
 *
 * global wpdb $wpdb
 *
 * @param int $comment_id Comment ID.
 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
 * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
{
    global $wpdb;
    switch ($comment_status) {
        case 'hold':
        case '0':
            $status = '0';
            break;
        case 'approve':
        case '1':
            $status = '1';
            if (get_option('comments_notify')) {
                wp_notify_postauthor($comment_id);
            }
            break;
        case 'spam':
            $status = 'spam';
            break;
        case 'trash':
            $status = 'trash';
            break;
        default:
            return false;
    }
    $comment_old = clone get_comment($comment_id);
    if (!$wpdb->update($wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id))) {
        if ($wp_error) {
            return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
        } else {
            return false;
        }
    }
    clean_comment_cache($comment_id);
    $comment = get_comment($comment_id);
    /**
     * Fires immediately before transitioning a comment's status from one to another
     * in the database.
     *
     * @since 1.5.0
     *
     * @param int         $comment_id     Comment ID.
     * @param string|bool $comment_status Current comment status. Possible values include
     *                                    'hold', 'approve', 'spam', 'trash', or false.
     */
    do_action('wp_set_comment_status', $comment_id, $comment_status);
    wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
    wp_update_comment_count($comment->comment_post_ID);
    return true;
}

WordPress Version: 4.1

/**
 * Sets the status of a comment.
 *
 * The 'wp_set_comment_status' action is called after the comment is handled.
 * If the comment status is not in the list, then false is returned.
 *
 * @since 1.0.0
 *
 * @param int $comment_id Comment ID.
 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
 * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
{
    global $wpdb;
    switch ($comment_status) {
        case 'hold':
        case '0':
            $status = '0';
            break;
        case 'approve':
        case '1':
            $status = '1';
            if (get_option('comments_notify')) {
                wp_notify_postauthor($comment_id);
            }
            break;
        case 'spam':
            $status = 'spam';
            break;
        case 'trash':
            $status = 'trash';
            break;
        default:
            return false;
    }
    $comment_old = clone get_comment($comment_id);
    if (!$wpdb->update($wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id))) {
        if ($wp_error) {
            return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
        } else {
            return false;
        }
    }
    clean_comment_cache($comment_id);
    $comment = get_comment($comment_id);
    /**
     * Fires immediately before transitioning a comment's status from one to another
     * in the database.
     *
     * @since 1.5.0
     *
     * @param int         $comment_id     Comment ID.
     * @param string|bool $comment_status Current comment status. Possible values include
     *                                    'hold', 'approve', 'spam', 'trash', or false.
     */
    do_action('wp_set_comment_status', $comment_id, $comment_status);
    wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
    wp_update_comment_count($comment->comment_post_ID);
    return true;
}

WordPress Version: 4.0

/**
 * Sets the status of a comment.
 *
 * The 'wp_set_comment_status' action is called after the comment is handled.
 * If the comment status is not in the list, then false is returned.
 *
 * @since 1.0.0
 * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
 *
 * @param int $comment_id Comment ID.
 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
 * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
{
    global $wpdb;
    switch ($comment_status) {
        case 'hold':
        case '0':
            $status = '0';
            break;
        case 'approve':
        case '1':
            $status = '1';
            if (get_option('comments_notify')) {
                wp_notify_postauthor($comment_id);
            }
            break;
        case 'spam':
            $status = 'spam';
            break;
        case 'trash':
            $status = 'trash';
            break;
        default:
            return false;
    }
    $comment_old = clone get_comment($comment_id);
    if (!$wpdb->update($wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id))) {
        if ($wp_error) {
            return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
        } else {
            return false;
        }
    }
    clean_comment_cache($comment_id);
    $comment = get_comment($comment_id);
    /**
     * Fires immediately before transitioning a comment's status from one to another
     * in the database.
     *
     * @since 1.5.0
     *
     * @param int         $comment_id     Comment ID.
     * @param string|bool $comment_status Current comment status. Possible values include
     *                                    'hold', 'approve', 'spam', 'trash', or false.
     */
    do_action('wp_set_comment_status', $comment_id, $comment_status);
    wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
    wp_update_comment_count($comment->comment_post_ID);
    return true;
}

WordPress Version: 3.8

/**
 * Sets the status of a comment.
 *
 * The 'wp_set_comment_status' action is called after the comment is handled.
 * If the comment status is not in the list, then false is returned.
 *
 * @since 1.0.0
 * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
 *
 * @param int $comment_id Comment ID.
 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
 * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
{
    global $wpdb;
    $status = '0';
    switch ($comment_status) {
        case 'hold':
        case '0':
            $status = '0';
            break;
        case 'approve':
        case '1':
            $status = '1';
            if (get_option('comments_notify')) {
                wp_notify_postauthor($comment_id);
            }
            break;
        case 'spam':
            $status = 'spam';
            break;
        case 'trash':
            $status = 'trash';
            break;
        default:
            return false;
    }
    $comment_old = clone get_comment($comment_id);
    if (!$wpdb->update($wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id))) {
        if ($wp_error) {
            return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
        } else {
            return false;
        }
    }
    clean_comment_cache($comment_id);
    $comment = get_comment($comment_id);
    /**
     * Fires after a comment status has been updated in the database.
     *
     * The hook also fires immediately before comment status transition hooks are fired.
     *
     * @since 1.5.0
     *
     * @param int         $comment_id     The comment ID.
     * @param string|bool $comment_status The comment status. Possible values include 'hold',
     *                                    'approve', 'spam', 'trash', or false.
     */
    do_action('wp_set_comment_status', $comment_id, $comment_status);
    wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
    wp_update_comment_count($comment->comment_post_ID);
    return true;
}

WordPress Version: 3.7

/**
 * Sets the status of a comment.
 *
 * The 'wp_set_comment_status' action is called after the comment is handled.
 * If the comment status is not in the list, then false is returned.
 *
 * @since 1.0.0
 * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
 *
 * @param int $comment_id Comment ID.
 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
 * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
{
    global $wpdb;
    $status = '0';
    switch ($comment_status) {
        case 'hold':
        case '0':
            $status = '0';
            break;
        case 'approve':
        case '1':
            $status = '1';
            if (get_option('comments_notify')) {
                $comment = get_comment($comment_id);
                wp_notify_postauthor($comment_id, $comment->comment_type);
            }
            break;
        case 'spam':
            $status = 'spam';
            break;
        case 'trash':
            $status = 'trash';
            break;
        default:
            return false;
    }
    $comment_old = clone get_comment($comment_id);
    if (!$wpdb->update($wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id))) {
        if ($wp_error) {
            return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
        } else {
            return false;
        }
    }
    clean_comment_cache($comment_id);
    $comment = get_comment($comment_id);
    do_action('wp_set_comment_status', $comment_id, $comment_status);
    wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
    wp_update_comment_count($comment->comment_post_ID);
    return true;
}