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
- Open your child theme’s
functions.phpvia Appearance > Theme File Editor, or use a snippets plugin like WPCode (Code Snippets > Add Snippet > PHP Snippet). - Paste the code below at the bottom of the file or into the snippet box.
- Change
5to the number of revisions you want to keep per post. - Click Update File, or set the snippet to Active and click Save.
- 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
- Each time you save a post, WordPress runs the
wp_revisions_to_keepfilter to decide how many revisions to retain. - The function returns a fixed number, so WordPress automatically deletes older revisions beyond that limit when you save.
- 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.
