How to Limit Post Revisions in WordPress
Home » WordPress » How to Limit Post Revisions in WordPress

How to Limit Post Revisions in WordPress

Controlling Post Revisions in WordPress: An Easy Guide – The default WordPress setting to save every revision can lead to a large database, which may affect performance over time.

This feature is useful for correcting mistakes, but it can be impractical if you only need a few backups of your posts.

This guide will explain how to limit the number of post revisions in WordPress using two simple methods, helping you to manage your database more efficiently.

Understanding Revisions in WordPress & Limit the Number of Revisions

Before we dive into the code, let’s talk about what revisions are. Each time you update a post, WordPress creates a revision—a backup copy of that post.

These revisions are stored as entries of the 'revision' post type in the wp_posts table, which can quickly lead to a bloated database. For performance and manageability, it makes sense to keep only a limited number of these backups.

Imagine you’re editing a post: you don’t need every single change saved forever. Keeping just the latest few revisions is usually sufficient, and it keeps your database lean and efficient.

Important Note:

Before applying the custom function to your theme’s functions.php file, we recommend you to create a WordPress child theme instead.


Method 1: Using the wp_revisions_to_keep Filter

The first method uses a WordPress filter to control the number of revisions kept for a post. we like this method because it’s flexible and allows you to target specific post types.

Setting Up the Revision Limit

We wrote a function that takes the current number of revisions and the post object as inputs. Then, we determine whether the post type is one that we want to limit (for example, posts and pages).

If it is, we return a fixed number (in our case, 5) to keep only the latest five revisions.

function zerobytecode_limit_revisions($num, $post){
    $N = 5; // Keep only the latest N revisions    
    $target_types = array('post', 'page');
    $is_target_type = in_array($post->post_type, $target_types);
    return $is_target_type ? $N : $num;
}
add_filter('wp_revisions_to_keep', 'zerobytecode_limit_revisions', 10, 2);

How It Works

Let us break down what’s happening here:

  • Function Definition:
    The function zerobytecode_limit_revisions takes two parameters: the current number of revisions ($num) and the $post object.
  • Defining the Limit:
    we set $N = 5 to indicate that we want to keep only five revisions.
  • Targeting Specific Post Types:
    we create an array, $target_types, containing 'post' and 'page'. Then, we check if the post’s type is in that array using in_array(). This way, we can selectively limit revisions only for those post types.
  • Returning the Limit:
    If the post type matches, the function returns $N (5), meaning only the latest five revisions are kept. If not, it returns the original number ($num), leaving other post types unchanged.

This approach gives you granular control over which post types should have their revisions limited.


Method 2: Defining the Revision Limit in wp-config.php

Another easy way to limit revisions is to set a constant in your wp-config.php file. This method is straightforward and applies globally to all posts that support revisions.

How to Implement It

All you need to do is open your wp-config.php file and add a single line of code:

define('WP_POST_REVISIONS', 5);

What It Does

  • Global Setting:
    This line tells WordPress to keep a maximum of 5 revisions for each post. It’s a quick fix if you want to apply the limit site-wide without writing additional code.
  • Override Order:
    Keep in mind that the wp_revisions_to_keep filter (Method 1) takes precedence over this setting. If you have both in place, WordPress will follow the filter’s instructions.

We personally appreciate this method for its simplicity when we just need a global rule without any complex conditions.


Practical Tips for Managing Revisions

Now that you know the two methods, here are some additional thoughts we always keep in mind:

  • Performance Matters:
    Limiting revisions helps reduce database bloat, which in turn can improve your site’s performance.
  • Backup Considerations:
    While it’s beneficial to limit revisions, make sure you have an alternative backup solution in case you need to restore previous versions of your posts.
  • Plugin Options:
    If you’re not comfortable editing code directly, there are plugins like WP Revisions Control or Perfmatters that offer an easy interface to manage revision limits.
  • Testing:
    After making changes, update a post several times and check the number of revisions. You can see the current revision count in the Publish box on the Edit Post screen.

Conclusion

In this guide, we’ve shown you how to limit the number of post revisions in WordPress using two methods. The first method uses the wp_revisions_to_keep filter for more granular control over specific post types, while the second method involves a simple constant in wp-config.php for a global setting.

Both approaches aim to reduce database clutter and improve your site’s performance. we hope you find these methods as effective and easy to implement as we do. Happy coding and enjoy a leaner, more efficient WordPress setup!

Similar Posts