Index: src/includes/common/functions.php
===================================================================
--- src/includes/common/functions.php	(revision 5286)
+++ src/includes/common/functions.php	(working copy)
@@ -1400,41 +1400,42 @@
  * @param int $parent_id Parent id
  * @param string $post_type Post type. Defaults to 'post'
  * @uses bbp_get_topic_post_type() To get the topic post type
- * @uses wp_cache_get() To check if there is a cache of the last child id
- * @uses wpdb::prepare() To prepare the query
- * @uses wpdb::get_var() To get the result of the query in a variable
- * @uses wp_cache_set() To set the cache for future use
+ * @uses WP_Query To get get the posts
  * @uses apply_filters() Calls 'bbp_get_public_child_last_id' with the child
  *                        id, parent id and post type
  * @return int The last active post_id
  */
 function bbp_get_public_child_last_id( $parent_id = 0, $post_type = 'post' ) {
-	global $wpdb;
 
 	// Bail if nothing passed
-	if ( empty( $parent_id ) )
+	if ( empty( $parent_id ) ) {
 		return false;
+	}
 
-	// The ID of the cached query
-	$cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_last_id';
+	// Get the public posts status
+	$post_status = array( bbp_get_public_status_id() );
 
+	// Add closed status if topic post type
+	if ( $post_type === bbp_get_topic_post_type() ) {
+		$post_status[] = bbp_get_closed_status_id();
+	}
+
 	// Check for cache and set if needed
-	$child_id = wp_cache_get( $cache_id, 'bbpress_posts' );
-	if ( false === $child_id ) {
-		$post_status = array( bbp_get_public_status_id() );
+	$query = new WP_Query( array(
+		'fields'      => 'ids',
+		'post_parent' => $parent_id,
+		'post_status' => $post_status,
+		'post_type'   => $post_type,
 
-		// Add closed status if topic post type
-		if ( $post_type === bbp_get_topic_post_type() ) {
-			$post_status[] = bbp_get_closed_status_id();
-		}
+		// Maybe change these later
+		'posts_per_page'         => 1,
+		'update_post_term_cache' => false,
+		'update_post_meta_cache' => false,
+		'ignore_sticky_posts'    => true,
+	) );
+	$child_id = array_shift( $query->posts );
+	unset( $query );
 
-		// Join post statuses together
-		$post_status = "'" . implode( "', '", $post_status ) . "'";
-
-		$child_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", $parent_id, $post_type ) );
-		wp_cache_set( $cache_id, $child_id, 'bbpress_posts' );
-	}
-
 	// Filter and return
 	return apply_filters( 'bbp_get_public_child_last_id', (int) $child_id, $parent_id, $post_type );
 }
@@ -1445,41 +1446,41 @@
  * @param int $parent_id Parent id
  * @param string $post_type Post type. Defaults to 'post'
  * @uses bbp_get_topic_post_type() To get the topic post type
- * @uses wp_cache_get() To check if there is a cache of the children count
- * @uses wpdb::prepare() To prepare the query
- * @uses wpdb::get_var() To get the result of the query in a variable
- * @uses wp_cache_set() To set the cache for future use
+ * @uses WP_Query To get get the posts
  * @uses apply_filters() Calls 'bbp_get_public_child_count' with the child
  *                        count, parent id and post type
  * @return int The number of children
  */
 function bbp_get_public_child_count( $parent_id = 0, $post_type = 'post' ) {
-	global $wpdb;
 
 	// Bail if nothing passed
-	if ( empty( $parent_id ) )
+	if ( empty( $parent_id ) ) {
 		return false;
+	}
 
-	// The ID of the cached query
-	$cache_id    = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_count';
+	// Check the public post status
+	$post_status = array( bbp_get_public_status_id() );
 
-	// Check for cache and set if needed
-	$child_count = wp_cache_get( $cache_id, 'bbpress_posts' );
-	if ( false === $child_count ) {
-		$post_status = array( bbp_get_public_status_id() );
+	// Add closed status if topic post type
+	if ( $post_type === bbp_get_topic_post_type() ) {
+		$post_status[] = bbp_get_closed_status_id();
+	}
 
-		// Add closed status if topic post type
-		if ( $post_type === bbp_get_topic_post_type() ) {
-			$post_status[] = bbp_get_closed_status_id();
-		}
+	$query = new WP_Query( array(
+		'fields'      => 'ids',
+		'post_parent' => $parent_id,
+		'post_status' => $post_status,
+		'post_type'   => $post_type,
 
-		// Join post statuses together
-		$post_status = "'" . implode( "', '", $post_status ) . "'";
+		// Maybe change these later
+		'posts_per_page'         => -1,
+		'update_post_term_cache' => false,
+		'update_post_meta_cache' => false,
+		'ignore_sticky_posts'    => true,
+	) );
+	$child_count = $query->post_count;
+	unset( $query );
 
-		$child_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $parent_id, $post_type ) );
-		wp_cache_set( $cache_id, $child_count, 'bbpress_posts' );
-	}
-
 	// Filter and return
 	return apply_filters( 'bbp_get_public_child_count', (int) $child_count, $parent_id, $post_type );
 }
@@ -1490,41 +1491,41 @@
  * @param int $parent_id Parent id
  * @param string $post_type Post type. Defaults to 'post'
  * @uses bbp_get_topic_post_type() To get the topic post type
- * @uses wp_cache_get() To check if there is a cache of the children
- * @uses wpdb::prepare() To prepare the query
- * @uses wpdb::get_col() To get the result of the query in an array
- * @uses wp_cache_set() To set the cache for future use
+ * @uses WP_Query To get get the posts
  * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids,
  *                        parent id and post type
  * @return array The array of children
  */
 function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) {
-	global $wpdb;
 
 	// Bail if nothing passed
-	if ( empty( $parent_id ) )
+	if ( empty( $parent_id ) ) {
 		return false;
+	}
 
-	// The ID of the cached query
-	$cache_id  = 'bbp_parent_public_' . $parent_id . '_type_' . $post_type . '_child_ids';
+	// Get the public post status
+	$post_status = array( bbp_get_public_status_id() );
 
-	// Check for cache and set if needed
-	$child_ids = wp_cache_get( $cache_id, 'bbpress_posts' );
-	if ( false === $child_ids ) {
-		$post_status = array( bbp_get_public_status_id() );
+	// Add closed status if topic post type
+	if ( $post_type === bbp_get_topic_post_type() ) {
+		$post_status[] = bbp_get_closed_status_id();
+	}
 
-		// Add closed status if topic post type
-		if ( $post_type === bbp_get_topic_post_type() ) {
-			$post_status[] = bbp_get_closed_status_id();
-		}
+	$query = new WP_Query( array(
+		'fields'      => 'ids',
+		'post_parent' => $parent_id,
+		'post_status' => $post_status,
+		'post_type'   => $post_type,
 
-		// Join post statuses together
-		$post_status = "'" . implode( "', '", $post_status ) . "'";
+		// Maybe change these later
+		'posts_per_page'         => -1,
+		'update_post_term_cache' => false,
+		'update_post_meta_cache' => false,
+		'ignore_sticky_posts'    => true,
+	) );
+	$child_ids = !empty( $query->posts ) ? $query->posts : array();
+	unset( $query );
 
-		$child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
-		wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );
-	}
-
 	// Filter and return
 	return apply_filters( 'bbp_get_public_child_ids', $child_ids, $parent_id, $post_type );
 }
@@ -1535,59 +1536,59 @@
  * @param int $parent_id Parent id
  * @param string $post_type Post type. Defaults to 'post'
  * @uses bbp_get_topic_post_type() To get the topic post type
- * @uses wp_cache_get() To check if there is a cache of the children
- * @uses wpdb::prepare() To prepare the query
- * @uses wpdb::get_col() To get the result of the query in an array
- * @uses wp_cache_set() To set the cache for future use
+ * @uses WP_Query To get get the posts
  * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids,
  *                        parent id and post type
  * @return array The array of children
  */
 function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) {
-	global $wpdb;
 
 	// Bail if nothing passed
-	if ( empty( $parent_id ) )
+	if ( empty( $parent_id ) ) {
 		return false;
+	}
 
-	// The ID of the cached query
-	$cache_id  = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids';
+	// Get the public post status
+	$post_status = array( bbp_get_public_status_id() );
 
-	// Check for cache and set if needed
-	$child_ids = wp_cache_get( $cache_id, 'bbpress_posts' );
-	if ( false === $child_ids ) {
-		$post_status = array( bbp_get_public_status_id() );
+	// Extra post statuses based on post type
+	switch ( $post_type ) {
 
-		// Extra post statuses based on post type
-		switch ( $post_type ) {
+		// Forum
+		case bbp_get_forum_post_type() :
+			$post_status[] = bbp_get_private_status_id();
+			$post_status[] = bbp_get_hidden_status_id();
+			break;
 
-			// Forum
-			case bbp_get_forum_post_type() :
-				$post_status[] = bbp_get_private_status_id();
-				$post_status[] = bbp_get_hidden_status_id();
-				break;
+		// Topic
+		case bbp_get_topic_post_type() :
+			$post_status[] = bbp_get_closed_status_id();
+			$post_status[] = bbp_get_trash_status_id();
+			$post_status[] = bbp_get_spam_status_id();
+			break;
 
-			// Topic
-			case bbp_get_topic_post_type() :
-				$post_status[] = bbp_get_closed_status_id();
-				$post_status[] = bbp_get_trash_status_id();
-				$post_status[] = bbp_get_spam_status_id();
-				break;
+		// Reply
+		case bbp_get_reply_post_type() :
+			$post_status[] = bbp_get_trash_status_id();
+			$post_status[] = bbp_get_spam_status_id();
+			break;
+	}
 
-			// Reply
-			case bbp_get_reply_post_type() :
-				$post_status[] = bbp_get_trash_status_id();
-				$post_status[] = bbp_get_spam_status_id();
-				break;
-		}
+	$query = new WP_Query( array(
+		'fields'      => 'ids',
+		'post_parent' => $parent_id,
+		'post_status' => 'any',
+		'post_type'   => $post_type,
 
-		// Join post statuses together
-		$post_status = "'" . implode( "', '", $post_status ) . "'";
+		// Maybe change these later
+		'posts_per_page'         => -1,
+		'update_post_term_cache' => false,
+		'update_post_meta_cache' => false,
+		'ignore_sticky_posts'    => true,
+	) );
+	$child_ids = !empty( $query->posts ) ? $query->posts : array();
+	unset( $query );
 
-		$child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
-		wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );
-	}
-
 	// Filter and return
 	return apply_filters( 'bbp_get_all_child_ids', $child_ids, (int) $parent_id, $post_type );
 }
Index: src/includes/core/cache.php
===================================================================
--- src/includes/core/cache.php	(revision 5286)
+++ src/includes/core/cache.php	(working copy)
@@ -145,22 +145,6 @@
 
 	do_action( 'bbp_clean_post_cache', $_post->ID, $_post );
 
-	// Child query types to clean
-	$post_types = array(
-		bbp_get_topic_post_type(),
-		bbp_get_forum_post_type(),
-		bbp_get_reply_post_type()
-	);
-
-	// Loop through query types and clean caches
-	foreach ( $post_types as $post_type ) {
-		wp_cache_delete( 'bbp_get_forum_'     . $_post->ID . '_reply_id',                              'bbpress_posts' );
-		wp_cache_delete( 'bbp_parent_'        . $_post->ID . '_type_' . $post_type . '_child_last_id', 'bbpress_posts' );
-		wp_cache_delete( 'bbp_parent_'        . $_post->ID . '_type_' . $post_type . '_child_count',   'bbpress_posts' );
-		wp_cache_delete( 'bbp_parent_public_' . $_post->ID . '_type_' . $post_type . '_child_ids',     'bbpress_posts' );
-		wp_cache_delete( 'bbp_parent_all_'    . $_post->ID . '_type_' . $post_type . '_child_ids',     'bbpress_posts' );
-	}
-
 	// Invalidate parent caches
 	if ( ! empty( $_post->post_parent ) ) {
 		bbp_clean_post_cache( $_post->post_parent );
Index: src/includes/forums/functions.php
===================================================================
--- src/includes/forums/functions.php	(revision 5286)
+++ src/includes/forums/functions.php	(working copy)
@@ -929,10 +929,10 @@
 	// First, delete everything.
 	delete_option( '_bbp_private_forums' );
 	delete_option( '_bbp_hidden_forums'  );
-	
+
 	/**
 	 * Don't search for both private/hidden statuses. Since 'pre_get_posts' is an
-	 * action, it's not removed by suppress_filters. We need to make sure that 
+	 * action, it's not removed by suppress_filters. We need to make sure that
 	 * we're only searching for the supplied post_status.
 	 *
 	 * @see https://bbpress.trac.wordpress.org/ticket/2512
@@ -954,7 +954,7 @@
 		'post_status'      => bbp_get_hidden_status_id(),
 		'fields'           => 'ids'
 	) );
-	
+
 	// Enable forum visibilty normalization
 	add_action( 'pre_get_posts', 'bbp_pre_get_posts_normalize_forum_visibility', 4 );
 
@@ -1449,7 +1449,6 @@
  * @return int Topic hidden topic count
  */
 function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = 0 ) {
-	global $wpdb;
 
 	// If topic_id was passed as $forum_id, then get its forum
 	if ( bbp_is_topic( $forum_id ) ) {
@@ -1466,8 +1465,20 @@
 
 		// Get topics of forum
 		if ( empty( $topic_count ) ) {
-			$post_status = "'" . implode( "','", array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ) ) . "'";
-			$topic_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $forum_id, bbp_get_topic_post_type() ) );
+			$query = new WP_Query( array(
+				'fields'      => 'ids',
+				'post_parent' => $forum_id,
+				'post_status' => array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ),
+				'post_type'   => bbp_get_topic_post_type(),
+
+				// Maybe change these later
+				'posts_per_page'         => -1,
+				'update_post_term_cache' => false,
+				'update_post_meta_cache' => false,
+				'ignore_sticky_posts'    => true,
+			) );
+			$topic_count = $query->post_count;
+			unset( $query );
 		}
 
 		// Update the count
@@ -1499,7 +1510,6 @@
  * @return int Forum reply count
  */
 function bbp_update_forum_reply_count( $forum_id = 0 ) {
-	global $wpdb;
 
 	$forum_id = bbp_get_forum_id( $forum_id );
 	$children_reply_count = 0;
@@ -1515,8 +1525,20 @@
 	// Don't count replies if the forum is a category
 	$topic_ids = bbp_forum_query_topic_ids( $forum_id );
 	if ( !empty( $topic_ids ) ) {
-		$topic_ids   = implode( ',', wp_parse_id_list( $topic_ids ) );
-		$reply_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
+		$query = new WP_Query( array(
+			'fields'          => 'ids',
+			'post_parent__in' => $topic_ids,
+			'post_status'     => bbp_get_public_status_id(),
+			'post_type'       => bbp_get_reply_post_type(),
+
+			// Maybe change these later
+			'posts_per_page'         => -1,
+			'update_post_term_cache' => false,
+			'update_post_meta_cache' => false,
+			'ignore_sticky_posts'    => true,
+		) );
+		$reply_count = ! empty( $query->posts ) ? count( $query->posts ) : 0;
+		unset( $query );
 	} else {
 		$reply_count = 0;
 	}
@@ -1904,26 +1926,11 @@
  */
 function bbp_forum_query_subforum_ids( $forum_id ) {
 	$subforum_ids = bbp_get_all_child_ids( $forum_id, bbp_get_forum_post_type() );
-	//usort( $subforum_ids, '_bbp_forum_query_usort_subforum_ids' );
 
 	return apply_filters( 'bbp_get_forum_subforum_ids', $subforum_ids, $forum_id );
 }
 
 /**
- * Callback to sort forum ID's based on last active time
- *
- * @since bbPress (r3789)
- * @param int $a First forum ID to compare
- * @param int $b Second forum ID to compare
- * @return Position change based on sort
- */
-function _bbp_forum_query_usort_subforum_ids( $a = 0, $b = 0 ) {
-	$ta = get_post_meta( $a, '_bbp_last_active_time', true );
-	$tb = get_post_meta( $b, '_bbp_last_active_time', true );
-	return ( $ta < $tb ) ? -1 : 1;
-}
-
-/**
  * Returns the forum's last reply id
  *
  * @since bbPress (r2908)
@@ -1930,35 +1937,35 @@
  *
  * @param int $forum_id Forum id
  * @param int $topic_ids Optional. Topic ids
- * @uses wp_cache_get() To check for cache and retrieve it
  * @uses bbp_forum_query_topic_ids() To get the forum's topic ids
- * @uses wpdb::prepare() To prepare the query
- * @uses wpdb::get_var() To execute the query and get the var back
  * @uses bbp_get_reply_post_type() To get the reply post type
- * @uses wp_cache_set() To set the cache for future use
  * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id
  *                        and forum id
  */
-function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) {
-	global $wpdb;
+function bbp_forum_query_last_reply_id( $forum_id = 0, $topic_ids = 0 ) {
 
-	$cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id';
-	$reply_id = (int) wp_cache_get( $cache_id, 'bbpress_posts' );
+	// Validate forum
+	$forum_id = bbp_get_forum_id( $forum_id );
 
-	if ( false === $reply_id ) {
+	// Get topic ID's if none were passed
+	if ( empty( $topic_ids ) ) {
+		$topic_ids = bbp_forum_query_topic_ids( $forum_id );
+	}
 
-		if ( empty( $topic_ids ) ) {
-			$topic_ids = bbp_forum_query_topic_ids( $forum_id );
-		}
+	$query = new WP_Query( array(
+		'fields'          => 'ids',
+		'post_parent__in' => $topic_ids,
+		'post_status'     => bbp_get_public_status_id(),
+		'post_type'       => bbp_get_reply_post_type(),
 
-		if ( !empty( $topic_ids ) ) {
-			$topic_ids = implode( ',', wp_parse_id_list( $topic_ids ) );
-			$reply_id  = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
-			wp_cache_set( $cache_id, $reply_id, 'bbpress_posts' ); // May be (int) 0
-		} else {
-			wp_cache_set( $cache_id, '0', 'bbpress_posts' );
-		}
-	}
+		// Maybe change these later
+		'posts_per_page'         => 1,
+		'update_post_term_cache' => false,
+		'update_post_meta_cache' => false,
+		'ignore_sticky_posts'    => true,
+	) );
+	$reply_id = array_shift( $query->posts );
+	unset( $query );
 
 	return (int) apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id );
 }
