Using WordPress ‘close_comments_for_post_types’ PHP filter

The close_comments_for_post_types WordPress PHP filter allows you to modify the list of post types for which comments should be automatically closed.

Usage

add_filter('close_comments_for_post_types', 'your_custom_function_name');
function your_custom_function_name($post_types) {
    // your custom code here

    return $post_types;
}

Parameters

  • $post_types (string[]): An array of post type names.

More information

See WordPress Developer Resources: close_comments_for_post_types

Examples

Close comments for custom post type “books”

Close comments automatically for the “books” custom post type:

add_filter('close_comments_for_post_types', 'close_comments_for_books');
function close_comments_for_books($post_types) {
    $post_types[] = 'books';
    return $post_types;
}

Close comments for pages

Automatically close comments for all pages:

add_filter('close_comments_for_post_types', 'close_comments_for_pages');
function close_comments_for_pages($post_types) {
    $post_types[] = 'page';
    return $post_types;
}

Close comments for multiple custom post types

Close comments for custom post types “books” and “movies”:

add_filter('close_comments_for_post_types', 'close_comments_for_multiple_post_types');
function close_comments_for_multiple_post_types($post_types) {
    $post_types[] = 'books';
    $post_types[] = 'movies';
    return $post_types;
}

Open comments for posts

Remove “post” from the list of post types to have open comments:

add_filter('close_comments_for_post_types', 'open_comments_for_posts');
function open_comments_for_posts($post_types) {
    $key = array_search('post', $post_types);
    if ($key !== false) {
        unset($post_types[$key]);
    }
    return $post_types;
}

Close comments for all post types

Close comments for all post types by returning an empty array:

add_filter('close_comments_for_post_types', 'close_comments_for_all_post_types');
function close_comments_for_all_post_types($post_types) {
    return array();
}