Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

Limit post revisions

Keep at most 5 revisions per post or page in WordPress, so your database doesn't fill up with old copies.

  • Type PHP
  • Runs Everywhere
PHP
// 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.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.