bbp_admin_repair_topic_voice_count()

Recount topic members

Description

Return

(array) An array of the status code and the message

Source

File: bp-forums/admin/tools.php

313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
function bbp_admin_repair_topic_voice_count() {
    global $wpdb;
 
    $statement = __( 'Counting the number of members in each discussion… %s', 'buddyboss' );
    $result    = __( 'Failed!', 'buddyboss' );
 
    $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_voice_count';";
    if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
        return array( 1, sprintf( $statement, $result ) );
 
    // Post types and status
    $tpt = bbp_get_topic_post_type();
    $rpt = bbp_get_reply_post_type();
    $pps = bbp_get_public_status_id();
    $cps = bbp_get_closed_status_id();
 
    $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
            SELECT `postmeta`.`meta_value`, '_bbp_voice_count', COUNT(DISTINCT `post_author`) as `meta_value`
                FROM `{$wpdb->posts}` AS `posts`
                LEFT JOIN `{$wpdb->postmeta}` AS `postmeta`
                    ON `posts`.`ID` = `postmeta`.`post_id`
                    AND `postmeta`.`meta_key` = '_bbp_topic_id'
                WHERE `posts`.`post_type` IN ( '{$tpt}', '{$rpt}' )
                    AND `posts`.`post_status` IN ( '{$pps}', '{$cps}' )
                    AND `posts`.`post_author` != '0'
                GROUP BY `postmeta`.`meta_value`);";
 
    if ( is_wp_error( $wpdb->query( $sql ) ) )
        return array( 2, sprintf( $statement, $result ) );
 
    return array( 0, sprintf( $statement, __( 'Complete!', 'buddyboss' ) ) );
}

Changelog

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