_close_comments_for_old_posts

函数
_close_comments_for_old_posts ( $posts, $query )
Access
Private
参数
  • (WP_Post) $posts Post data object.
    Required:
  • (WP_Query) $query Query object.
    Required:
返回值
  • (array)
定义位置
相关方法
_close_comments_for_old_postwp_latest_comments_draft_or_post_titlewp_set_comment_statuspost_comments_form_block_form_defaultswp_check_comment_flood
引入
2.7.0
弃用
-

close_comments_for_old_posts: 这个函数类似于close_comments_for_old_post,但它接受一个可选的$post_type参数来指定文章类型。

即时关闭旧文章的评论,没有任何额外的数据库查询。与the_posts挂钩。

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;
}

常见问题

FAQs
查看更多 >