_close_comments_for_old_posts

Close comments on old posts on the fly, without any extra DB queries.

Description

(array) _close_comments_for_old_posts( (WP_Post) $posts, (WP_Query) $query ); 

Hooked to the_posts.

Returns (array)

Parameters (2)

0. $posts (WP_Post)
Post data object.
1. $query (WP_Query)
Query object.

Usage

  1. if ( !function_exists( '_close_comments_for_old_posts' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // Post data object. 
  5. $posts = null; 
  6.  
  7. // Query object. 
  8. $query = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = _close_comments_for_old_posts($posts, $query); 
  12.  

Defined (1)

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

/wp-includes/comment.php  
  1. function _close_comments_for_old_posts( $posts, $query ) { 
  2. if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) ) 
  3. return $posts; 
  4.  
  5. /** 
  6. * Filters the list of post types to automatically close comments for. 
  7. * @since 3.2.0 
  8. * @param array $post_types An array of registered post types. Default array with 'post'. 
  9. */ 
  10. $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); 
  11. if ( ! in_array( $posts[0]->post_type, $post_types ) ) 
  12. return $posts; 
  13.  
  14. $days_old = (int) get_option( 'close_comments_days_old' ); 
  15. if ( ! $days_old ) 
  16. return $posts; 
  17.  
  18. if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) { 
  19. $posts[0]->comment_status = 'closed'; 
  20. $posts[0]->ping_status = 'closed'; 
  21.  
  22. return $posts;