WordPress: Disable Comments on Old Posts Automatically
Stop spam on old WordPress posts by closing comments automatically after a set number of days.
Stop WordPress filling your database with revisions: keep only 5 revisions per post and slow autosave to every 5 minutes.
10 lines · Updated Nov 26, 2026
<?php
// Keep at most 5 revisions per post.
add_filter( 'wp_revisions_to_keep', function ( $num, $post ) {
return 5;
}, 10, 2 );
// Autosave every 5 minutes instead of every 60 seconds.
if ( ! defined( 'AUTOSAVE_INTERVAL' ) ) {
define( 'AUTOSAVE_INTERVAL', 300 );
}
Every time you press Save or Update, WordPress stores a complete copy of the post as a revision — and it never deletes them on its own. On an active blog that quietly adds thousands of rows to the wp_posts table, which makes backups bigger, database queries slower and migrations more painful. This snippet caps how many revisions WordPress keeps and makes autosave run less often, so your database stays lean without losing the ability to undo recent changes.
The limit only applies to future saves. To remove old revisions, use WP-CLI (wp post delete $(wp post list --post_type=revision --format=ids) --force) or a trusted database cleanup plugin — after taking a backup.
0 or false disables revisions — use a small positive number instead.How many revisions should I keep? Three to ten is a sensible range for most blogs. Sites with many editors may want more.
Does this speed up my site? It mainly keeps the database smaller and backups faster. Front-end speed improvements are usually small but database-heavy admin pages benefit.
Related: 5 code snippets every developer needs · Disable comments on old posts
Add it to your child theme's functions.php, or — safer — paste it into the free Code Snippets plugin, where you can turn it on and off without touching any file. Never edit your parent theme's functions.php directly: one typo can lock you out of your site, and a theme update wipes your changes. After adding it, edit any post, save it a few times, and check that only 5 revisions are listed under the Revisions panel.
Share this snippet
Stop spam on old WordPress posts by closing comments automatically after a set number of days.
Add your own widget to the WordPress dashboard — show today's orders, a checklist, or quick links for your team.
Log every 404 on your WordPress site so you can see which broken links visitors actually hit — and fix them.