bbp_trash_topic( $topic_id )

Called before trashing a topic

Description

This function is supplemental to the actual topic being trashed which is handled by WordPress core API functions. It is used to clean up after a topic that is being trashed.

Source

File: bp-forums/topics/functions.php

3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
function bbp_trash_topic( $topic_id = 0 ) {
 
    // Validate topic ID
    $topic_id = bbp_get_topic_id( $topic_id );
 
    if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
        return false;
 
    do_action( 'bbp_trash_topic', $topic_id );
 
    // Topic is being trashed, so its replies are trashed too
    $replies = new WP_Query( array(
        'suppress_filters' => true,
        'post_type'        => bbp_get_reply_post_type(),
        'post_status'      => bbp_get_public_status_id(),
        'post_parent'      => $topic_id,
        'posts_per_page'   => -1,
        'nopaging'         => true,
        'fields'           => 'id=>parent'
    ) );
 
    if ( !empty( $replies->posts ) ) {
 
        // Prevent debug notices
        $pre_trashed_replies = array();
 
        // Loop through replies, trash them, and add them to array
        foreach ( $replies->posts as $reply ) {
            wp_trash_post( $reply->ID );
            $pre_trashed_replies[] = $reply->ID;
        }
 
        // Set a post_meta entry of the replies that were trashed by this action.
        // This is so we can possibly untrash them, without untrashing replies
        // that were purposefully trashed before.
        update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies );
 
        // Reset the $post global
        wp_reset_postdata();
    }
 
    // Cleanup
    unset( $replies );
}

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.