Index: includes/admin/tools.php
===================================================================
--- includes/admin/tools.php	(revision 5264)
+++ includes/admin/tools.php	(working copy)
@@ -262,7 +262,7 @@
 				LEFT JOIN `{$wpdb->postmeta}` AS `postmeta`
 					ON `posts`.`ID` = `postmeta`.`post_id`
 					AND `postmeta`.`meta_key` = '_bbp_topic_id'
-				WHERE `posts`.`post_type` IN ( '{$tpt}', '{$rpt}' )
+				WHERE `posts`.`post_type` = '{$tpt}'
 					AND `posts`.`post_status` IN ( '{$pps}', '{$cps}' )
 					AND `posts`.`post_author` != '0'
 				GROUP BY `postmeta`.`meta_value`);";
@@ -908,42 +908,28 @@
 			WHERE `topic`.`meta_id` IS NULL AND `forum`.`post_type` = '{$fpt}' );" ) ) )
 		return array( 5, sprintf( $statement, $result ) );
 
-	// After that, we give all the topics with replies the ID their last reply (again, this time for a different reason).
+	// Give all the topics with replies the ID of their last reply or topics without a reply an ID of themself.
 	if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
-			( SELECT `topic`.`ID`, '_bbp_last_active_id', MAX( `reply`.`ID` )
-			FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
-			WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
-			GROUP BY `topic`.`ID` );" ) ) )
+			( SELECT `topic`.`ID`,  '_bbp_last_active_id', COALESCE( ( SELECT MAX( `reply`.`ID` )
+															FROM `$wpdb->posts` AS `reply`
+															WHERE `topic`.`ID` = `reply`.`post_parent` AND `reply`.`post_status` = '{$pps}') , `topic`.`ID` )
+			FROM `$wpdb->posts` AS `topic`
+			WHERE `topic`.`post_type` = '{$tpt}' );" ) ) )
 		return array( 6, sprintf( $statement, $result ) );
 
-	// For any remaining topics, give a reply ID of themself.
+	// Give topics with or without replies their last update time.
 	if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
-			( SELECT `ID`, '_bbp_last_active_id', `ID`
-			FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
-			ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_id'
-			WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' );" ) ) )
+			( SELECT `topic`.`ID`,  '_bbp_last_active_time', COALESCE( ( SELECT MAX( `reply`.`post_date` )
+															FROM `$wpdb->posts` AS `reply`
+															WHERE `topic`.`ID` = `reply`.`post_parent` AND `reply`.`post_status` = '{$pps}') , `topic`.`post_date` )
+			FROM `$wpdb->posts` AS `topic`
+			WHERE `topic`.`post_type` = '{$tpt}' );" ) ) )
 		return array( 7, sprintf( $statement, $result ) );
 
-	// Give topics with replies their last update time.
-	if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
-			( SELECT `topic`.`ID`, '_bbp_last_active_time', MAX( `reply`.`post_date` )
-			FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
-			WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
-			GROUP BY `topic`.`ID` );" ) ) )
-		return array( 8, sprintf( $statement, $result ) );
-
-	// Give topics without replies their last update time.
-	if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
-			( SELECT `ID`, '_bbp_last_active_time', `post_date`
-			FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
-			ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_time'
-			WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' );" ) ) )
-		return array( 9, sprintf( $statement, $result ) );
-
 	// Forums need to know what their last active item is as well. Now it gets a bit more complex to do in the database.
 	$forums = $wpdb->get_col( "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type` = '{$fpt}' and `post_status` != 'auto-draft';" );
 	if ( is_wp_error( $forums ) )
-		return array( 10, sprintf( $statement, $result ) );
+		return array( 8, sprintf( $statement, $result ) );
 
  	// Loop through forums
  	foreach ( $forums as $forum_id ) {
