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

_close_comments_for_old_posts() WordPress Function

The close_comments_for_old_posts() function is used to close comments on a WordPress post after a certain amount of time has passed. This can be useful for preventing comment spam on old posts, or simply to keep the comments section on your website tidy. By default, comments will be closed on a post 30 days after it is published.

_close_comments_for_old_posts( WP_Post $posts, WP_Query $query ) #

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


Parameters

$posts

(WP_Post)(Required)Post data object.

$query

(WP_Query)(Required)Query object.


Top ↑

Return

(array)


Top ↑

Source

File: wp-includes/comment.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;
}


Top ↑

Changelog

Changelog
VersionDescription
2.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More