bbp_update_topic_walker( int $topic_id, string $last_active_time = '', int $forum_id, int $reply_id, bool $refresh = true )

Walks up the post_parent tree from the current topic_id, and updates the counts of forums above it. This calls a few internal functions that all run manual queries against the database to get their results. As such, this function can be costly to run but is necessary to keep everything accurate.

Description

Parameters

$topic_id

(Required) Topic id

$last_active_time

(Optional) Last active time

Default value: ''

$forum_id

(Optional) Forum id

$reply_id

(Optional) Reply id

$refresh

(Optional) Reset all the previous parameters? Defaults to true.

Default value: true

Source

File: bp-forums/topics/functions.php

function bbp_update_topic_walker( $topic_id, $last_active_time = '', $forum_id = 0, $reply_id = 0, $refresh = true ) {

	// Validate topic_id
	$topic_id  = bbp_get_topic_id( $topic_id );

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

	// Topic was passed
	if ( !empty( $topic_id ) ) {

		// Get the forum ID if none was passed
		if ( empty( $forum_id )  ) {
			$forum_id = bbp_get_topic_forum_id( $topic_id );
		}

		// Set the active_id based on topic_id/reply_id
		$active_id = empty( $reply_id ) ? $topic_id : $reply_id;
	}

	// Get topic ancestors
	$ancestors = array_values( array_unique( array_merge( array( $forum_id ), (array) get_post_ancestors( $topic_id ) ) ) );

	// Topic status
	$topic_status = get_post_status( $topic_id );

	// If we want a full refresh, unset any of the possibly passed variables
	if ( true === $refresh ) {
		$forum_id = $topic_id = $reply_id = $active_id = $last_active_time = 0;
		$topic_status = bbp_get_public_status_id();
	}

	// Loop through ancestors
	if ( !empty( $ancestors ) ) {
		foreach ( $ancestors as $ancestor ) {

			// If ancestor is a forum, update counts
			if ( bbp_is_forum( $ancestor ) ) {

				// Update the forum
				bbp_update_forum( array(
					'forum_id'           => $ancestor,
					'last_topic_id'      => $topic_id,
					'last_reply_id'      => $reply_id,
					'last_active_id'     => $active_id,
					'last_active_time'   => 0,
					'last_active_status' => $topic_status
				) );
			}
		}
	}
}

Changelog

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