bbp_update_forum_last_topic_id( int $forum_id, int $topic_id )

Update the forum last topic id

Description

Parameters

$forum_id

(int) (Optional) Forum id

$topic_id

(int) (Optional) Topic id

Return

(bool) True on success, false on failure

Source

File: bp-forums/forums/functions.php

function bbp_update_forum_last_topic_id( $forum_id = 0, $topic_id = 0 ) {
	$forum_id = bbp_get_forum_id( $forum_id );

	// Define local variable(s)
	$children_last_topic = 0;

	// Do some calculation if not manually set
	if ( empty( $topic_id ) ) {

		// 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_last_topic = bbp_update_forum_last_topic_id( $child ); // Recursive
			}
		}

		// Setup recent topic query vars
		$post_vars = array(
			'post_parent' => $forum_id,
			'post_type'   => bbp_get_topic_post_type(),
			'meta_key'    => '_bbp_last_active_time',
			'orderby'     => 'meta_value',
			'numberposts' => 1
		);

		// Get the most recent topic in this forum_id
		$recent_topic = get_posts( $post_vars );
		if ( !empty( $recent_topic ) ) {
			$topic_id = $recent_topic[0]->ID;
		}
	}

	// Cast as integer in case of empty or string
	$topic_id            = (int) $topic_id;
	$children_last_topic = (int) $children_last_topic;

	// If child forums have higher id, use that instead
	if ( !empty( $children ) && ( $children_last_topic > $topic_id ) )
		$topic_id = $children_last_topic;

	// Update the last public topic ID
	if ( bbp_is_topic_published( $topic_id ) )
		update_post_meta( $forum_id, '_bbp_last_topic_id', $topic_id );

	return (int) apply_filters( 'bbp_update_forum_last_topic_id', $topic_id, $forum_id );
}

Changelog

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