← Back to blog

How to disable comments in WordPress, including the posts the setting does not reach

The SandyWP team 5 min read

To disable comments in WordPress, uncheck Allow people to submit comments on new posts in Settings, Discussion, then close comments on the posts that already exist with Bulk actions, Edit, Comments, Do not allow. The first step alone changes nothing you can see, because the setting only applies to posts created after you save it.

To switch comments off everywhere at once, including media files and anything a bot posts directly, you need one line of code or a plugin. Both are below.

Every guide we read covers the clicks. None of the four (WPBeginner, InstaWP, WP Engine and Compete Themes) explains why spam keeps arriving after you have "disabled" comments, which is the reason most people come back to this question.

Everything below is checked against the WordPress 7.1.4 source.

Why are comments still open after I unchecked the setting?

Because open or closed is stored on each post, not on the site. Every row in wp_posts has a comment_status column holding open or closed, and a ping_status column for pingbacks and trackbacks.

The Discussion option, default_comment_status, is only the value WordPress copies into that column when a post is created.

The settings screen says so under the checkboxes: "Individual posts may override these settings. Changes here will only be applied to new posts."

comments_open(), the function every theme and every comment endpoint asks, reads the post's own column. It never looks at the option.

How do I disable comments on existing posts?

  1. Go to Posts, All Posts and set Number of items per page in Screen Options high enough to show them all.
  2. Tick the select-all checkbox, choose Edit from Bulk actions, and click Apply.
  3. Set Comments to Do not allow. Set Pings to Do not allow while you are there.
  4. Click Update.

For one post, open it in the editor and untick Allow comments in the post sidebar's Discussion panel.

Repeat the bulk edit for any custom post type that supports comments. Products, courses and portfolio items each carry their own comment_status.

Do pages have comments turned on?

Usually not. get_default_comment_status() returns closed for the page post type no matter what the Discussion setting says, so pages are created with comments off.

A page can still be open if someone ticked it by hand, or if it was imported from another site with its status intact. The bulk edit on Pages, All Pages fixes that the same way.

Why am I still getting spam after disabling comments?

There are four common reasons, roughly in order of how often they turn up.

Media files are still open. An attachment is a post too, the attachment post type supports comments, and uploads get the default status when they are created. Every image uploaded while the setting was on has comment_status = open, and the Media library has no bulk edit for it.

New installs since WordPress 6.4 turn attachment pages off (wp_attachment_pages_enabled is 0), so there is no form to see. Sites upgraded from older versions had the option set to 1 by the upgrade routine, and keep their attachment pages.

Either way, the form was never the target. Bots post straight to wp-comments-post.php with an attachment's ID, and wp_handle_comment_submission() only checks whether that post is open.

Pingbacks and trackbacks are a separate switch. They are governed by ping_status and pings_open(), not by the comment setting. A post with comments closed and pings open still accepts a pingback over XML-RPC or a trackback to wp-trackback.php, and both land in the Comments screen.

"Automatically close comments on old posts" only covers posts. The filter behind it, _close_comments_for_old_post(), applies to the post types in close_comments_for_post_types, and the default list is post alone. Pages, attachments and custom post types stay open forever.

The spam is old. Closing comments stops new submissions. It does not delete the pending and spam comments already in the database, and notification emails for those can keep a plugin like Akismet busy.

How do I disable comments everywhere with code?

Every route a stranger can post through asks the same two functions. The comment form handler, the REST API's create-comment permission check and XML-RPC's wp.newComment all call comments_open(); the pingback handler and wp-trackback.php call pings_open().

Both functions end in a filter, so forcing them to false closes everything at once, whatever each post's column says:

<?php
/**
 * Plugin Name: Disable comments
 */
add_filter( 'comments_open', '__return_false', 20 );
add_filter( 'pings_open', '__return_false', 20 );

// Hide comments that already exist.
add_filter( 'comments_array', '__return_empty_array' );

// Remove the Comments menu from wp-admin.
add_action( 'admin_menu', function () {
    remove_menu_page( 'edit-comments.php' );
} );

Save it as wp-content/mu-plugins/disable-comments.php. A must-use plugin loads before themes, cannot be deactivated from the Plugins screen, and survives a theme switch, which a snippet in functions.php does not.

Two honest caveats. Removing the menu only hides the screen; the comments are still in wp_comments.

And this is an override, not a cleanup: delete the file and every post goes back to whatever its column says.

The free Disable Comments plugin does the same job with a settings screen, and lets you choose post types. Use it if you would rather not maintain a file.

How do I hide the comments that are already on my site?

Closing comments does not hide existing ones. In a classic theme, comments_template() still prints them, followed by a "Comments are closed" line if the theme has one.

In a block theme, the Comments block returns nothing only when comments are closed and the post has zero comments. With even one approved comment, the list still renders, and the form simply disappears.

You have three choices:

  • Delete them. Comments, All Comments, select all, Move to Trash, then empty the trash.
  • Filter them out with comments_array, as in the snippet above. Nothing is lost if you change your mind.
  • Remove the block. In a block theme, open Appearance, Editor, Templates, edit the Single Posts template and delete the Comments block. That edit is saved per theme, so it does not follow you to a new one.

How do I disable comments with WP-CLI?

Over SSH, this closes comments and pings on everything that is open, media included:

# Stop new posts defaulting to open
wp option update default_comment_status closed
wp option update default_ping_status closed

# Close posts, pages and anything else that is open
wp post update $(wp post list --post_type=any --post_status=any \
  --comment_status=open --format=ids) \
  --comment_status=closed --ping_status=closed

# Media files have their own status, inherit
wp post update $(wp post list --post_type=attachment --post_status=inherit \
  --comment_status=open --format=ids) \
  --comment_status=closed --ping_status=closed

comment_status and ping_status are real WP_Query arguments, so wp post list filters on them directly. If a list comes back empty, wp post update complains about a missing ID; that just means there was nothing left to close.

To clear the backlog, wp comment list --status=spam --format=ids feeds wp comment delete --force the same way. Swap spam for hold to empty the moderation queue.

Does disabling comments affect anything else?

A few things, mostly harmless:

  • Comment feeds. The post comments feed link in <head> is only printed when comments or pings are open, or the post already has comments.
  • Cached pages. A page cache keeps serving the old form until it expires. Clear the cache and check as a logged-out visitor.
  • XML-RPC. Closing pings stops pingbacks, but the endpoint still answers. If you want it gone, our XML-RPC post covers turning it off and what breaks when you do.

Should I test this on a copy first?

For unticking a box on a five-post blog, no. Closing comments is easy to undo.

A copy earns its place when the change is bulk and one-way: deleting thousands of comments, running the WP-CLI loop above on a client's site, or adding an mu-plugin to a shop where product reviews are comments too. That last one is the trap: WooCommerce reviews are comments on product posts, and a blanket comments_open filter switches them off with everything else.

The SandyWP Cloner copies a live site's database and wp-content into a disposable sandbox, and only reads from production. Every comment_status value, every attachment and every pending comment comes across, so the copy fails the same way the live site would.

From there, SSH access runs the WP-CLI commands against the copy, and the Email Log captures the moderation and notification emails WordPress sends, so you can see which ones stop. If the result is right, repeat it on the live site; if not, delete the sandbox.