Limit post revisions
Keep at most 5 revisions per post or page in WordPress, so your database doesn't fill up with old copies.
// Keep at most 5 revisions per post. Post types without revisions stay at 0.
add_filter(
'wp_revisions_to_keep',
function ( $num ) {
$max = 5;
return ( $num < 0 || $num > $max ) ? $max : $num;
}
);
What it does
WordPress keeps every saved version of a post as a revision, with no limit. Posts edited often can build up hundreds of revisions and bloat your database.
This snippet keeps the 5 newest revisions for each post. Change $max to keep more or fewer. If revisions are already off, or wp-config.php sets a lower limit, that setting stays.
Good to know
- Extra revisions are deleted the next time each post is saved, not straight away.