bbp_admin_repair_topic_reply_count()
Recount topic replies
Description
Return
(array) An array of the status code and the message
Source
File: bp-forums/admin/tools.php
function bbp_admin_repair_topic_reply_count() {
global $wpdb;
$statement = __( 'Counting the number of replies in each discussion… %s', 'buddyboss' );
$result = __( 'Failed!', 'buddyboss' );
// 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();
// Delete the meta key _bbp_reply_count for each topic
$sql_delete = "DELETE `postmeta` FROM `{$wpdb->postmeta}` AS `postmeta`
LEFT JOIN `{$wpdb->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
WHERE `posts`.`post_type` = '{$tpt}'
AND `postmeta`.`meta_key` = '_bbp_reply_count'";
if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
return array( 1, sprintf( $statement, $result ) );
}
// Recalculate the meta key _bbp_reply_count for each topic
$sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
SELECT `topics`.`ID` AS `post_id`, '_bbp_reply_count' AS `meta_key`, COUNT(`replies`.`ID`) As `meta_value`
FROM `{$wpdb->posts}` AS `topics`
LEFT JOIN `{$wpdb->posts}` as `replies`
ON `replies`.`post_parent` = `topics`.`ID`
AND `replies`.`post_status` = '{$pps}'
AND `replies`.`post_type` = '{$rpt}'
WHERE `topics`.`post_type` = '{$tpt}'
AND `topics`.`post_status` IN ( '{$pps}', '{$cps}' )
GROUP BY `topics`.`ID`);";
if ( is_wp_error( $wpdb->query( $sql ) ) ) {
return array( 2, sprintf( $statement, $result ) );
}
return array( 0, sprintf( $statement, __( 'Complete!', 'buddyboss' ) ) );
}
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.