bbp_update_forum_last_topic_id( int $forum_id, int $topic_id )

Update the forum last topic id

Description

Parameters

$forum_id

(Optional) Forum id

$topic_id

(Optional) Topic id

Return

(bool) True on success, false on failure

Source

File: bp-forums/forums/functions.php

1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
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.