bbp_admin_repair_topic_reply_count

Recount topic replies.

Description

(array) bbp_admin_repair_topic_reply_count(); 

Returns (array)

An array of the status code and the message


Usage

  1. if ( !function_exists( 'bbp_admin_repair_topic_reply_count' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/admin/tools.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bbp_admin_repair_topic_reply_count(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/admin/tools.php  
  1. function bbp_admin_repair_topic_reply_count() { 
  2. global $wpdb; 
  3.  
  4. $statement = __( 'Counting the number of replies in each topic… %s', bbpress ); 
  5. $result = __( 'Failed!', bbpress ); 
  6.  
  7. // Post types and status 
  8.  
  9. // Delete the meta key _bbp_reply_count for each topic 
  10. $sql_delete = "DELETE `postmeta` FROM `{$wpdb->postmeta}` AS `postmeta` 
  11. LEFT JOIN `{$wpdb->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id` 
  12. WHERE `posts`.`post_type` = '{$tpt}' 
  13. AND `postmeta`.`meta_key` = '_bbp_reply_count'"; 
  14.  
  15. if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) { 
  16. return array( 1, sprintf( $statement, $result ) ); 
  17.  
  18. // Recalculate the meta key _bbp_reply_count for each topic 
  19. $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) ( 
  20. SELECT `topics`.`ID` AS `post_id`, '_bbp_reply_count' AS `meta_key`, COUNT(`replies`.`ID`) As `meta_value` 
  21. FROM `{$wpdb->posts}` AS `topics` 
  22. LEFT JOIN `{$wpdb->posts}` as `replies` 
  23. ON `replies`.`post_parent` = `topics`.`ID` 
  24. AND `replies`.`post_status` = '{$pps}' 
  25. AND `replies`.`post_type` = '{$rpt}' 
  26. WHERE `topics`.`post_type` = '{$tpt}' 
  27. AND `topics`.`post_status` IN ( '{$pps}', '{$cps}' ) 
  28. GROUP BY `topics`.`ID`);"; 
  29.  
  30. if ( is_wp_error( $wpdb->query( $sql ) ) ) { 
  31. return array( 2, sprintf( $statement, $result ) ); 
  32.  
  33. return array( 0, sprintf( $statement, __( 'Complete!', bbpress ) ) );