The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Limit Post Revisions

Shared by Amit Dholiya · November 23, 2026 · @wp_revisions_to_keep

6 views
1 upvote
Back to Snippets

Limits how many revisions WordPress stores for each post or page. By default WordPress keeps unlimited revisions, which can bloat the database and slow down the admin over time. This snippet caps it at a number you choose.

How to add it

  1. Open your child theme’s functions.php via Appearance > Theme File Editor, or use a snippets plugin like WPCode (Code Snippets > Add Snippet > PHP Snippet).
  2. Paste the code below at the bottom of the file or into the snippet box.
  3. Change 5 to the number of revisions you want to keep per post.
  4. Click Update File, or set the snippet to Active and click Save.
  5. Edit and update a post several times, then check the Revisions panel in the editor to confirm only the latest ones are kept.

How it works

  1. Each time you save a post, WordPress runs the wp_revisions_to_keep filter to decide how many revisions to retain.
  2. The function returns a fixed number, so WordPress automatically deletes older revisions beyond that limit when you save.
  3. Existing old revisions are cleaned up the next time each post is updated.
/**
 * Limit the number of post revisions stored.
 *
 * @param int     $num  Default number of revisions to keep.
 * @param WP_Post $post Post object being saved.
 * @return int Modified number of revisions.
 */
function my_limit_post_revisions( $num, $post ) {
    return 5; // Number of revisions to keep
}
add_filter( 'wp_revisions_to_keep', 'my_limit_post_revisions', 10, 2 );

Tested on

WordPress 6.8

Reported by the author on November 23, 2026. WPFolks has not verified this.

Not tested against WordPress 7.1.5. It may still work.

Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments

Proudly supported by