wp_trash_post_comments

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

WordPress Version: 5.6

/**
 * Moves comments for a post to the Trash.
 *
 * @since 2.9.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
 * @return mixed|void False on failure.
 */
function wp_trash_post_comments($post = null)
{
    global $wpdb;
    $post = get_post($post);
    if (!$post) {
        return;
    }
    $post_id = $post->ID;
    /**
     * Fires before comments are sent to the Trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('trash_post_comments', $post_id);
    $comments = $wpdb->get_results($wpdb->prepare("SELECT comment_ID, comment_approved FROM {$wpdb->comments} WHERE comment_post_ID = %d", $post_id));
    if (!$comments) {
        return;
    }
    // Cache current status for each comment.
    $statuses = array();
    foreach ($comments as $comment) {
        $statuses[$comment->comment_ID] = $comment->comment_approved;
    }
    add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
    // Set status for all comments to post-trashed.
    $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id));
    clean_comment_cache(array_keys($statuses));
    /**
     * Fires after comments are sent to the Trash.
     *
     * @since 2.9.0
     *
     * @param int   $post_id  Post ID.
     * @param array $statuses Array of comment statuses.
     */
    do_action('trashed_post_comments', $post_id, $statuses);
    return $result;
}

WordPress Version: 5.4

/**
 * Moves comments for a post to the Trash.
 *
 * @since 2.9.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
 * @return mixed|void False on failure.
 */
function wp_trash_post_comments($post = null)
{
    global $wpdb;
    $post = get_post($post);
    if (empty($post)) {
        return;
    }
    $post_id = $post->ID;
    /**
     * Fires before comments are sent to the Trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('trash_post_comments', $post_id);
    $comments = $wpdb->get_results($wpdb->prepare("SELECT comment_ID, comment_approved FROM {$wpdb->comments} WHERE comment_post_ID = %d", $post_id));
    if (empty($comments)) {
        return;
    }
    // Cache current status for each comment.
    $statuses = array();
    foreach ($comments as $comment) {
        $statuses[$comment->comment_ID] = $comment->comment_approved;
    }
    add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
    // Set status for all comments to post-trashed.
    $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id));
    clean_comment_cache(array_keys($statuses));
    /**
     * Fires after comments are sent to the Trash.
     *
     * @since 2.9.0
     *
     * @param int   $post_id  Post ID.
     * @param array $statuses Array of comment statuses.
     */
    do_action('trashed_post_comments', $post_id, $statuses);
    return $result;
}

WordPress Version: 4.3

/**
 * Moves comments for a post to the trash.
 *
 * @since 2.9.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
 * @return mixed|void False on failure.
 */
function wp_trash_post_comments($post = null)
{
    global $wpdb;
    $post = get_post($post);
    if (empty($post)) {
        return;
    }
    $post_id = $post->ID;
    /**
     * Fires before comments are sent to the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('trash_post_comments', $post_id);
    $comments = $wpdb->get_results($wpdb->prepare("SELECT comment_ID, comment_approved FROM {$wpdb->comments} WHERE comment_post_ID = %d", $post_id));
    if (empty($comments)) {
        return;
    }
    // Cache current status for each comment.
    $statuses = array();
    foreach ($comments as $comment) {
        $statuses[$comment->comment_ID] = $comment->comment_approved;
    }
    add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
    // Set status for all comments to post-trashed.
    $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id));
    clean_comment_cache(array_keys($statuses));
    /**
     * Fires after comments are sent to the trash.
     *
     * @since 2.9.0
     *
     * @param int   $post_id  Post ID.
     * @param array $statuses Array of comment statuses.
     */
    do_action('trashed_post_comments', $post_id, $statuses);
    return $result;
}

WordPress Version: 4.1

/**
 * Moves comments for a post to the trash.
 *
 * @since 2.9.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
 * @return mixed False on failure.
 */
function wp_trash_post_comments($post = null)
{
    global $wpdb;
    $post = get_post($post);
    if (empty($post)) {
        return;
    }
    $post_id = $post->ID;
    /**
     * Fires before comments are sent to the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('trash_post_comments', $post_id);
    $comments = $wpdb->get_results($wpdb->prepare("SELECT comment_ID, comment_approved FROM {$wpdb->comments} WHERE comment_post_ID = %d", $post_id));
    if (empty($comments)) {
        return;
    }
    // Cache current status for each comment.
    $statuses = array();
    foreach ($comments as $comment) {
        $statuses[$comment->comment_ID] = $comment->comment_approved;
    }
    add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
    // Set status for all comments to post-trashed.
    $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id));
    clean_comment_cache(array_keys($statuses));
    /**
     * Fires after comments are sent to the trash.
     *
     * @since 2.9.0
     *
     * @param int   $post_id  Post ID.
     * @param array $statuses Array of comment statuses.
     */
    do_action('trashed_post_comments', $post_id, $statuses);
    return $result;
}

WordPress Version: 4.0

/**
 * Moves comments for a post to the trash.
 *
 * @since 2.9.0
 *
 * @global wpdb $wpdb WordPress database access abstraction object.
 *
 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
 * @return mixed False on failure.
 */
function wp_trash_post_comments($post = null)
{
    global $wpdb;
    $post = get_post($post);
    if (empty($post)) {
        return;
    }
    $post_id = $post->ID;
    /**
     * Fires before comments are sent to the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('trash_post_comments', $post_id);
    $comments = $wpdb->get_results($wpdb->prepare("SELECT comment_ID, comment_approved FROM {$wpdb->comments} WHERE comment_post_ID = %d", $post_id));
    if (empty($comments)) {
        return;
    }
    // Cache current status for each comment.
    $statuses = array();
    foreach ($comments as $comment) {
        $statuses[$comment->comment_ID] = $comment->comment_approved;
    }
    add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
    // Set status for all comments to post-trashed.
    $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id));
    clean_comment_cache(array_keys($statuses));
    /**
     * Fires after comments are sent to the trash.
     *
     * @since 2.9.0
     *
     * @param int   $post_id  Post ID.
     * @param array $statuses Array of comment statuses.
     */
    do_action('trashed_post_comments', $post_id, $statuses);
    return $result;
}

WordPress Version: 3.9

/**
 * Moves comments for a post to the trash
 *
 * @since 2.9.0
 *
 * @param int|WP_Post $post Optional. Post ID or post object.
 * @return mixed False on failure
 */
function wp_trash_post_comments($post = null)
{
    global $wpdb;
    $post = get_post($post);
    if (empty($post)) {
        return;
    }
    $post_id = $post->ID;
    /**
     * Fires before comments are sent to the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('trash_post_comments', $post_id);
    $comments = $wpdb->get_results($wpdb->prepare("SELECT comment_ID, comment_approved FROM {$wpdb->comments} WHERE comment_post_ID = %d", $post_id));
    if (empty($comments)) {
        return;
    }
    // Cache current status for each comment
    $statuses = array();
    foreach ($comments as $comment) {
        $statuses[$comment->comment_ID] = $comment->comment_approved;
    }
    add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
    // Set status for all comments to post-trashed
    $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id));
    clean_comment_cache(array_keys($statuses));
    /**
     * Fires after comments are sent to the trash.
     *
     * @since 2.9.0
     *
     * @param int   $post_id  Post ID.
     * @param array $statuses Array of comment statuses.
     */
    do_action('trashed_post_comments', $post_id, $statuses);
    return $result;
}

WordPress Version: 3.7

/**
 * Moves comments for a post to the trash
 *
 * @since 2.9.0
 * @uses do_action() on 'trash_post_comments' before trashing
 * @uses do_action() on 'trashed_post_comments' after trashing
 *
 * @param int|object $post Post ID or object.
 * @return mixed False on failure
 */
function wp_trash_post_comments($post = null)
{
    global $wpdb;
    $post = get_post($post);
    if (empty($post)) {
        return;
    }
    $post_id = $post->ID;
    do_action('trash_post_comments', $post_id);
    $comments = $wpdb->get_results($wpdb->prepare("SELECT comment_ID, comment_approved FROM {$wpdb->comments} WHERE comment_post_ID = %d", $post_id));
    if (empty($comments)) {
        return;
    }
    // Cache current status for each comment
    $statuses = array();
    foreach ($comments as $comment) {
        $statuses[$comment->comment_ID] = $comment->comment_approved;
    }
    add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
    // Set status for all comments to post-trashed
    $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id));
    clean_comment_cache(array_keys($statuses));
    do_action('trashed_post_comments', $post_id, $statuses);
    return $result;
}