bbp_update_forum_reply_count( int $forum_id )

Adjust the total reply count of a forum

Description

Parameters

$forum_id

(int) (Optional) Forum id or topic id. It is checked whether it is a topic or a forum. If it's a topic, its parent, i.e. the forum is automatically retrieved.

$total_count

(bool) (Optional) To return the total count or normal count?

Return

(int) Forum reply count

Source

File: bp-forums/forums/functions.php

function bbp_update_forum_reply_count( $forum_id = 0 ) {
	global $wpdb;

	$forum_id = bbp_get_forum_id( $forum_id );
	$children_reply_count = 0;

	// Loop through children and add together forum reply counts
	$children = bbp_forum_query_subforum_ids( $forum_id );
	if ( !empty( $children ) ) {
		foreach ( (array) $children as $child ) {
			$children_reply_count += bbp_update_forum_reply_count( $child );
		}
	}

	// 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() ) );
	} else {
		$reply_count = 0;
	}

	// Calculate total replies in this forum
	$total_replies = (int) $reply_count + $children_reply_count;

	// Update the count
	update_post_meta( $forum_id, '_bbp_reply_count',       (int) $reply_count   );
	update_post_meta( $forum_id, '_bbp_total_reply_count', (int) $total_replies );

	return (int) apply_filters( 'bbp_update_forum_reply_count', (int) $total_replies, $forum_id );
}

Changelog

Changelog
Version Description
bbPress (r2464) Introduced.

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.