Code Snippet to Limit Post Revisions in WordPress

Janek S. Jan 5, 2025 Post Revisions
How can I stop my blog from saving so many old versions of my posts?
What code snippet can I use to limit the number of post revisions stored in the WordPress database by modifying the `wp-config.php` file?
Andy answered Jan 5, 2025

Limiting Post Revisions in WordPress

Custom Code Solution

To limit post revisions, add one of these code snippets to your wp-config.php file. Place it before the line that says /* That's all, stop editing! Happy publishing. */

Set a specific number of revisions (example shows 5 revisions):

define('WP_POST_REVISIONS', 5);

To completely disable revisions:

define('WP_POST_REVISIONS', false);

To set unlimited revisions (default WordPress behavior):

define('WP_POST_REVISIONS', true);

Alternative Database Cleanup Solution

If you want to remove existing revisions, add this code to your theme's functions.php file:

function clean_post_revisions() {
    global $wpdb;
    $wpdb->query(
        "DELETE FROM {$wpdb->posts} WHERE post_type = 'revision'"
    );
}
add_action('admin_init', 'clean_post_revisions');

Plugin Solutions

If you prefer using a plugin, here are reliable options:

  1. Revision Control - Lets you control the number of revisions per post type
  2. WP-Optimize - Includes revision management along with other database optimization features

Additional Notes

  • The wp-config.php solution is the most efficient as it prevents unnecessary revisions from being created
  • Changes to wp-config.php affect all future posts
  • Setting too few revisions might limit your ability to recover content
  • A reasonable number of revisions is typically between 5 and 10

Remember to backup your database before making any changes that affect revisions.