bbp_update_forum_reply_count( int $forum_id )

Adjust the total reply count of a forum

Description

Parameters

$forum_id

(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

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

Return

(int) Forum reply count

Source

File: bp-forums/forums/functions.php

1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
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.