bbp_bump_forum_topic_count( int $forum_id, int $difference = 1, bool $update_ancestors = true )

Bump the total topic count of a forum

Description

Parameters

$forum_id

(int) (Optional) Forum id.

$difference

(int) (Optional) Default 1

Default value: 1

$update_ancestors

(bool) (Optional) Default true

Default value: true

Return

(int) Forum topic count

Source

File: bp-forums/forums/functions.php

function bbp_bump_forum_topic_count( $forum_id = 0, $difference = 1, $update_ancestors = true ) {

	// Get some counts
	$forum_id          = bbp_get_forum_id( $forum_id );
	$topic_count       = bbp_get_forum_topic_count( $forum_id, false, true );
	$total_topic_count = bbp_get_forum_topic_count( $forum_id, true,  true );

	// Update this forum id
	update_post_meta( $forum_id, '_bbp_topic_count',       (int) $topic_count       + (int) $difference );
	update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topic_count + (int) $difference );

	// Check for ancestors
	if ( true === $update_ancestors ) {

		// Get post ancestors
		$forum     = get_post( $forum_id );
		$ancestors = get_post_ancestors( $forum );

		// If has ancestors, loop through them...
		if ( !empty( $ancestors ) ) {
			foreach ( (array) $ancestors as $parent_forum_id ) {

				// Get forum counts
				$parent_topic_count       = bbp_get_forum_topic_count( $parent_forum_id, false, true );
				$parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true,  true );

				// Update counts
				update_post_meta( $parent_forum_id, '_bbp_topic_count',       (int) $parent_topic_count       + (int) $difference );
				update_post_meta( $parent_forum_id, '_bbp_total_topic_count', (int) $parent_total_topic_count + (int) $difference );
			}
		}
	}

	return (int) apply_filters( 'bbp_bump_forum_topic_count', (int) $total_topic_count + (int) $difference, $forum_id, (int) $difference, (bool) $update_ancestors );
}

Changelog

Changelog
Version Description
bbPress (r3825) 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.