How to Limit the Number of Revisions Kept for a Post in WordPress
A revision is a backup of a post at a specific time.
Revisions
If revisions are enabled for a post, WordPress creates a new revision for the post each time the post is updated. This feature is useful to track changes and to restore the post to a previous state.
Revisions are instances of the 'revision'
post type. They are stored in the table wp_posts. This means that the table wp_posts grows quickly when this feature is enabled. For performance reasons, I recommend disabling revisions if you are not using the benefits of this feature.
If your scenario requires revisions, the best choice is to limit the number of revisions that WordPress keeps for a post. In this case, WordPress keeps in the database only the latest \(N\) revisions of the post.
Method 1
Use the 'wp_revisions_to_keep'
filter to control the number of revisions to keep for a post. The callback receives the current number of revisions to keep for the post, and an object of class WP_Post
that represents the post.
The callback should return the new number of revisions to keep for the post, which is interpreted as follows:
- Zero —disable revisions
- Greater than zero —keep this number of revisions
- Lower than zero —keep an unlimited number of revisions
The following code sets a maximum of five revisions for entries of the post types Post and Page.
/*
* Sets the number of revisions to keep for the post types Post and Page.
*/
function ns_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', 'ns_limit_revisions', 10, 2);
Method 2
Add the following code to the file wp-config.php and WordPress will keep a maximum of five revisions for entries of the post types that support revisions. Change this number as desired according to the meaning stated in the previous section.
// Keep a maximum of five revisions
define('WP_POST_REVISIONS', 5);
The 'wp_revisions_to_keep'
filter has precedence over this setting.
User interface
If revisions are enabled for a post and there are revisions for the post, WordPress shows the current number of revisions in the Publish box of the Edit Post screen.
Update the post title or the post content several times, and verify that the number of revisions is always lower than or equal to the established limit.
Further reading
I recommend the other tutorials in this series to learn more about revisions in WordPress.
- How to Disable Revisions in WordPress
- How to Limit the Number of Revisions Kept for a Post in WordPress
Exercise
Propose a solution to disable revisions for all post types except the post types Post and Page. In the case of posts, WordPress should keep a maximum of seven revisions. In the case of pages, only the latest five revisions should be kept.
Source code
The source code developed in this tutorial, including a possible answer to the exercise, is available here.
Comments