Title: _close_comments_for_old_posts
Published: April 25, 2014
Last modified: February 24, 2026

---

# _close_comments_for_old_posts( WP_Post[] $posts, WP_Query $query ): 󠀁[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)󠁿[]

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Closes comments on old posts on the fly, without any extra DB queries. Hooked to
the_posts.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#parameters)󠁿

 `$posts`[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)[]
required

Array of post objects.

`$query`[WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)required

Query object.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#return)󠁿

 [WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)[]

## 󠀁[Source](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#source)󠁿

    ```php
    function _close_comments_for_old_posts( $posts, $query ) {
    	if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) ) {
    		return $posts;
    	}

    	/**
    	 * Filters the list of post types to automatically close comments for.
    	 *
    	 * @since 3.2.0
    	 *
    	 * @param string[] $post_types An array of post type names.
    	 */
    	$post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
    	if ( ! in_array( $posts[0]->post_type, $post_types, true ) ) {
    		return $posts;
    	}

    	$days_old = (int) get_option( 'close_comments_days_old' );
    	if ( ! $days_old ) {
    		return $posts;
    	}

    	if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) {
    		$posts[0]->comment_status = 'closed';
    		$posts[0]->ping_status    = 'closed';
    	}

    	return $posts;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/comment.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/comment.php#L3500)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/comment.php#L3500-L3528)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#hooks)󠁿

 [apply_filters( ‘close_comments_for_post_types’, string[] $post_types )](https://developer.wordpress.org/reference/hooks/close_comments_for_post_types/)

Filters the list of post types to automatically close comments for.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#related)󠁿

| Uses | Description | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [get_option()](https://developer.wordpress.org/reference/functions/get_option/)`wp-includes/option.php` |

Retrieves an option value based on an option name.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.7.0](https://developer.wordpress.org/reference/since/2.7.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/?output_format=md#comment-content-7155)
 2.   [azadehnoory](https://profiles.wordpress.org/azadehnoory/)  [  2 years ago  ](https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/#comment-7155)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_close_comments_for_old_posts%2F%23comment-7155)
    Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_close_comments_for_old_posts%2F%23comment-7155)
 4. Automatically disable comments on old custom post types:
 5.     ```php
        add_filter( 'close_comments_for_post_types', 'wpdocs_close_comments' );
        function wpdocs_close_comments() : array {
            return array( 'your_old_custom_post_type' );
        }
        ```
    
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_close_comments_for_old_posts%2F%3Freplytocom%3D7155%23feedback-editor-7155)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_close_comments_for_old_posts%2F)
before being able to contribute a note or feedback.