get_pending_comments_num

Get the number of pending comments on a post or posts.

Description

(int|array) get_pending_comments_num( (int|array) $post_id ); 

Returns (int|array)

Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs

Parameters (1)

0. $post_id (int|array)
Either a single Post ID or an array of Post IDs

Usage

  1. if ( !function_exists( 'get_pending_comments_num' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/comment.php'; 
  3.  
  4. // Either a single Post ID or an array of Post IDs 
  5. $post_id = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = get_pending_comments_num($post_id); 
  9.  

Defined (1)

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

/wp-admin/includes/comment.php  
  1. function get_pending_comments_num( $post_id ) { 
  2. global $wpdb; 
  3.  
  4. $single = false; 
  5. if ( !is_array($post_id) ) { 
  6. $post_id_array = (array) $post_id; 
  7. $single = true; 
  8. } else { 
  9. $post_id_array = $post_id; 
  10. $post_id_array = array_map('intval', $post_id_array); 
  11. $post_id_in = "'" . implode("', '", $post_id_array) . "'"; 
  12.  
  13. $pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' GROUP BY comment_post_ID", ARRAY_A ); 
  14.  
  15. if ( $single ) { 
  16. if ( empty($pending) ) 
  17. return 0; 
  18. else 
  19. return absint($pending[0]['num_comments']); 
  20.  
  21. $pending_keyed = array(); 
  22.  
  23. // Default to zero pending for all posts in request 
  24. foreach ( $post_id_array as $id ) 
  25. $pending_keyed[$id] = 0; 
  26.  
  27. if ( !empty($pending) ) 
  28. foreach ( $pending as $pend ) 
  29. $pending_keyed[$pend['comment_post_ID']] = absint($pend['num_comments']); 
  30.  
  31. return $pending_keyed;