bbp_delete_topic_replies( int $topic_id )
Delete replies to a topic when it’s deleted
Description Description
Usually you’ll want to do this before the topic itself is deleted.
Parameters Parameters
- $topic_id
-
(Required)
Source Source
File: includes/topics/functions.php
function bbp_delete_topic_replies( $topic_id = 0 ) {
// Validate topic ID
$topic_id = bbp_get_topic_id( $topic_id );
// Topic is being permanently deleted, so its replies gotta go too
// Note that we get all post statuses here
$replies = new WP_Query( array(
'fields' => 'id=>parent',
'post_type' => bbp_get_reply_post_type(),
'post_status' => array_keys( get_post_stati() ),
'post_parent' => $topic_id,
'posts_per_page' => -1,
// Performance
'nopaging' => true,
'suppress_filters' => true,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'ignore_sticky_posts' => true,
'no_found_rows' => true
) );
// Loop through and delete child replies
if ( ! empty( $replies->posts ) ) {
foreach ( $replies->posts as $reply ) {
wp_delete_post( $reply->ID, true );
}
// Reset the $post global
wp_reset_postdata();
}
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |