bbp_move_topic_handler( int $topic_id, int $old_forum_id, int $new_forum_id )
Handle the moving of a topic from one forum to another. This includes walking up the old and new branches and updating the counts.
Description
Parameters
- $topic_id
-
(int) (Required) Topic id
- $old_forum_id
-
(int) (Required) Old forum id
- $new_forum_id
-
(int) (Required) New forum id
Source
File: bp-forums/topics/functions.php
function bbp_move_topic_handler( $topic_id, $old_forum_id, $new_forum_id ) { // Validate parameters $topic_id = bbp_get_topic_id( $topic_id ); $old_forum_id = bbp_get_forum_id( $old_forum_id ); $new_forum_id = bbp_get_forum_id( $new_forum_id ); // Update topic forum's ID bbp_update_topic_forum_id( $topic_id, $new_forum_id ); /** Stickies **************************************************************/ // Get forum stickies $old_stickies = bbp_get_stickies( $old_forum_id ); // Only proceed if stickies are found if ( !empty( $old_stickies ) ) { // Define local variables $updated_stickies = array(); // Loop through stickies of forum and add misses to the updated array foreach ( (array) $old_stickies as $sticky_topic_id ) { if ( $topic_id !== $sticky_topic_id ) { $updated_stickies[] = $sticky_topic_id; } } // If stickies are different, update or delete them if ( $updated_stickies !== $old_stickies ) { // No more stickies so delete the meta if ( empty( $updated_stickies ) ) { delete_post_meta( $old_forum_id, '_bbp_sticky_topics' ); // Still stickies so update the meta } else { update_post_meta( $old_forum_id, '_bbp_sticky_topics', $updated_stickies ); } // Topic was sticky, so restick in new forum bbp_stick_topic( $topic_id ); } } /** Topic Replies *********************************************************/ // Get the topics replies $replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() ); // Update the forum_id of all replies in the topic foreach ( $replies as $reply_id ) { bbp_update_reply_forum_id( $reply_id, $new_forum_id ); } /** Old forum_id **********************************************************/ // Get topic ancestors $old_forum_ancestors = array_values( array_unique( array_merge( array( $old_forum_id ), (array) get_post_ancestors( $old_forum_id ) ) ) ); // Loop through ancestors and update them if ( !empty( $old_forum_ancestors ) ) { foreach ( $old_forum_ancestors as $ancestor ) { if ( bbp_is_forum( $ancestor ) ) { bbp_update_forum( array( 'forum_id' => $ancestor, ) ); } } } /** New forum_id **********************************************************/ // Make sure we're not walking twice if ( !in_array( $new_forum_id, $old_forum_ancestors ) ) { // Get topic ancestors $new_forum_ancestors = array_values( array_unique( array_merge( array( $new_forum_id ), (array) get_post_ancestors( $new_forum_id ) ) ) ); // Make sure we're not walking twice $new_forum_ancestors = array_diff( $new_forum_ancestors, $old_forum_ancestors ); // Loop through ancestors and update them if ( !empty( $new_forum_ancestors ) ) { foreach ( $new_forum_ancestors as $ancestor ) { if ( bbp_is_forum( $ancestor ) ) { bbp_update_forum( array( 'forum_id' => $ancestor, ) ); } } } } }
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.