WordPress stores a complete copy of a post every time it’s saved, whether through autosave, manual save, or a publish action. With no limit set, a post edited frequently can accumulate hundreds of revisions in the wp_posts table. Each revision is a full row in the database with the complete post content, title, excerpt, and meta. On a site with thousands of posts and years of editing history, the revision bloat can dwarf the actual content in the database, slowing down backups, queries, and exports.
The Code
Add this to your functions.php or a site-specific plugin. Two options are provided: defining a constant in wp-config.php (preferred) or using the wp_revisions_to_keep filter (more flexible).
Option 1: WP_POST_REVISIONS Constant
Defining WP_POST_REVISIONS in wp-config.php sets a global limit that applies to all post types. It loads at the earliest point in WordPress’s bootstrap, before any plugins or themes run, making it the most reliable method. Set it to a positive integer to limit revisions, to 0 to disable revisions entirely (not recommended, revisions are a safety net for accidental data loss), or to false to re-enable unlimited revisions if a plugin has set a limit you want to override.
Option 2: wp_revisions_to_keep Filter
The filter approach gives you per-post-type control that the constant can’t provide. Blog posts where content is frequently refined might warrant 10 revisions. Snippet library posts that are mostly code and rarely change might need only 3. The filter receives both the current limit and the post object, so you can apply logic based on post type, post age, post author, or any other post attribute.
The ?? 5 fallback at the end of the return statement applies a default of 5 revisions to any post type not explicitly listed in the $limits array, ensuring all post types are covered.
Cleaning Up Existing Revisions
Setting a revision limit only affects new saves, it doesn’t retroactively delete existing revisions. To clean up the accumulated revision backlog, use WP-CLI: wp post delete $(wp post list --post_type='revision' --format=ids) --force. This permanently deletes all existing revisions. Take a database backup before running this on a production site. Alternatively, plugins like WP-Sweep provide a GUI for cleaning up revisions without WP-CLI.
What’s a Sensible Limit
Five to ten revisions covers almost every practical recovery scenario. In most cases, if you need to revert content you’ll know within a day or two, more than ten revisions of recoverability typically exists only in theory. The performance and storage benefit of capping at five is real; the lost recovery capability is theoretical.
/**
* Option 1: Set via wp-config.php (recommended, loads earliest)
* Add this line to wp-config.php above the 'stop editing' comment:
*
* define( 'WP_POST_REVISIONS', 5 );
*
* Set to 0 to disable revisions entirely.
* Set to false to enable unlimited revisions (WordPress default).
*/
/**
* Option 2: Set via filter (use if you can't edit wp-config.php,
* or if you need different limits per post type)
*/
add_filter( 'wp_revisions_to_keep', function( $num, $post ) {
// Different limits per post type
$limits = [
'post' => 10,
'page' => 5,
'nahnu_snippet' => 3,
'product' => 5,
];
return $limits[ $post->post_type ] ?? 5; // Default to 5 for any unspecified type
}, 10, 2 );
