wp_trash_post()

You are here:

wp_trash_post( int $post_id )

Move a post or page to the Trash

Description Description

If Trash is disabled, the post or page is permanently deleted.

See also See also

  • wp_delete_post()

Top ↑

Parameters Parameters

$post_id

(int) (Optional) Post ID. Default is ID of the global $post if EMPTY_TRASH_DAYS equals true.


Top ↑

Return Return

(WP_Post|false|null) Post data on success, false or null on failure.


Top ↑

Source Source

File: wp-includes/post.php

3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
function wp_trash_post( $post_id = 0 ) {
    if ( ! EMPTY_TRASH_DAYS ) {
        return wp_delete_post( $post_id, true );
    }
 
    $post = get_post( $post_id );
 
    if ( ! $post ) {
        return $post;
    }
 
    if ( 'trash' === $post->post_status ) {
        return false;
    }
 
    /**
     * Filters whether a post trashing should take place.
     *
     * @since 4.9.0
     *
     * @param bool|null $trash Whether to go forward with trashing.
     * @param WP_Post   $post  Post object.
     */
    $check = apply_filters( 'pre_trash_post', null, $post );
    if ( null !== $check ) {
        return $check;
    }
 
    /**
     * Fires before a post is sent to the Trash.
     *
     * @since 3.3.0
     *
     * @param int $post_id Post ID.
     */
    do_action( 'wp_trash_post', $post_id );
 
    add_post_meta( $post_id, '_wp_trash_meta_status', $post->post_status );
    add_post_meta( $post_id, '_wp_trash_meta_time', time() );
 
    $post_updated = wp_update_post(
        array(
            'ID'          => $post_id,
            'post_status' => 'trash',
        )
    );
 
    if ( ! $post_updated ) {
        return false;
    }
 
    wp_trash_post_comments( $post_id );
 
    /**
     * Fires after a post is sent to the Trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action( 'trashed_post', $post_id );
 
    return $post;
}


Top ↑

Changelog Changelog

Changelog
VersionDescription
2.9.0Introduced.
Was this article helpful?
Dislike 0
Views: 3

Cart

Log in

Create an Account
Back to Top