Limit post revisions

Keeps the database from filling up with hundreds of old revisions.

Hook
wp_revisions_to_keep
Updated
2026-09-27
License
GPLv2
add_filter( 'wp_revisions_to_keep', fn() => 5 );

These are the default values. Change them in the builder before you copy. Licensed under GPL-2.0-or-later, provided as is. Review and test it before using it on a live site.

How it works

WordPress keeps every revision of every post and page, forever. On a busy site that’s thousands of rows in wp_posts. The wp_revisions_to_keep filter tells WordPress how many to keep per post, for every post type that has revisions.

Good to know

  • Old revisions aren’t deleted all at once. WordPress trims each post down to the limit the next time that post is saved.
  • 0 turns revisions off. -1 means unlimited, the default.
  • The filter wins over the WP_POST_REVISIONS constant in wp-config.php, if you have one.

Set the number in the builder, under Revisions per post, before you copy the code.

How to check it works

Edit a post with many revisions and save it. The Revisions panel should now list no more than your limit.

How to undo it

Remove the code. Revisions already deleted don’t come back.

How to add this snippet

  1. Copy the code above.
  2. Paste it at the end of your child theme’s functions.php, in Appearance → Theme File Editor or over FTP.
  3. Save, then open the site and check that everything works. If something breaks, remove the code.

Need more than one? Open this snippet in the builder, turn on the others you want and copy them as one functions.php, or download them as a small plugin.

More in Performance