How to Take Control of Page and Post Revisions in WordPress

Share this article

How to Take Control of Page and Post Revisions in WordPress
Control of Page and Post Revisions in WordPress

This article is part of a series created in partnership with SiteGround. Thank you for supporting the partners who make SitePoint possible.

WordPress provides a revisions system which records a full copy of every page and post when it’s saved. The advantage: you can revert to an earlier version of the document at any time and make comparisons. Or you could discover who’s to blame for spolling and errors grammatical. By default, there is no limit to the number of revisions stored per page or post. (Note only a single auto-save is made per post per editor — the most recent auto-save overwrites the previous one.) Every revision requires a separate row in WordPress’s posts table and perhaps multiple entries in the postmeta and term_relationships tables. That’s rarely a problem for smaller sites but it could affect the performance and efficiency of larger installations. Tables eventually become filled with redundant data which will never be used.

Limiting Revisions

The number of revisions can be set in WordPress’s wp-config.php file. If you’re not aware of that file, I recommend you seek further guidance from a developer. Make a copy of wp-config.php before editing because the smallest error could break your WordPress site. To disable revisions entirely, add the following line to wp-config.php:
define('WP_POST_REVISIONS', 0);
To limit revisions, change the number to a positive integer, e.g. for no more than ten revisions per page/post:
define('WP_POST_REVISIONS', 10);
To revert back to unlimited revisions, remove the line or change the value to -1:
define('WP_POST_REVISIONS', -1);

Revision Plugins

If file editing feels a little too hardcore, there are several plugins to control revisions. For example, WP Revisions Limit sets the revision limit in an identical way.

Programmatically Limiting Revisions

The wp_revisions_to_keep filter allows plugins or your theme’s functions.php file to control how many revisions are retained for a given post. The filter’s function is passed two arguments:
  • the default number of revisions to keep
  • the WP_Post object of the current post
and must return the number of permitted revisions. The following example sets a limit of five revisions for posts with the type “custom_post”:
add_filter( 'wp_revisions_to_keep', 'control_revisions', 10, 2 );

function control_revisions($num, $post) {

  if('custom_post' == $post->post_type) $num = 5;
  return $num;

}
You can also use the WordPress REST API to list, retrieve and delete revisions.

How to Remove Old Revisions

The methods above activate immediately so, ideally, you should set WP_POST_REVISIONS shortly after installing WordPress. However, the setting will not remove old revisions from your MySQL database. It is possible to clean the old data but please be aware of the danger. Before taking any action, remember to… BACK UP YOUR DATABASE!
The easiest option is to use a plugin such as WP-Optimize which cleans your WordPress database by removing revisions as well as other optimizations. You can run it once or set a regular schedule. Alternatively, you can live dangerously and run a SQL command to clean revisions. First, find your WordPress table prefix — it is specified in wp-config.php, e.g.
$table_prefix = 'wp_';
wp_ is the default. We’ll assume wp_ has been specified for the following code but change the references if necessary. To delete all revisions for all pages and posts, start a MySQL administration tool such as phpMyAdmin and run the following SQL command:
DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN wp_term_taxonomy d ON ( b.term_taxonomy_id = d.term_taxonomy_id )
WHERE a.post_type = 'revision'
AND d.taxonomy != 'link_category';
Thanks to Michael Ambrosio for providing this fix when my original code, ahem, broke some stuff! All going well, you’ll have a sparkling clean database and WordPress will be noticeably faster. Alternatively, WordPress won’t start and you’ve lost a decade-worth of amazing posts. But you did back-up first, of course…

Frequently Asked Questions about WordPress Post Revision Control

How can I limit the number of revisions stored in WordPress?

WordPress automatically saves all revisions, but you can limit this number for better site performance. To do this, you need to add a line of code to your wp-config.php file. Use the following code: define(‘WP_POST_REVISIONS’, 3);. This will limit the number of revisions to 3. Remember to replace ‘3’ with the number of revisions you want to keep.

Can I completely disable WordPress revisions?

Yes, you can completely disable WordPress revisions by adding the following line of code to your wp-config.php file: define(‘WP_POST_REVISIONS’, false);. This will stop WordPress from storing any revisions, but it won’t delete the existing ones.

How can I delete old revisions in WordPress?

To delete old revisions, you can use a plugin like WP-Optimize or Better Delete Revision. These plugins will help you clean up your database and improve your website’s performance.

What is the difference between autosave and revisions in WordPress?

Autosave is a feature in WordPress that automatically saves a draft of your post as you’re writing. It’s different from revisions because it doesn’t create a new revision each time it saves. Instead, it overwrites the previous autosave.

Can I restore a previous version of a post using revisions?

Yes, you can easily restore a previous version of a post using the WordPress revisions feature. Simply go to the post editor, click on ‘Revisions’, and you’ll see a list of all revisions. You can compare different versions and restore the one you want.

How can I compare different revisions in WordPress?

WordPress has a built-in feature that allows you to compare different revisions. In the post editor, click on ‘Revisions’, and you’ll see a screen where you can compare different versions of your post. You can use the slider at the top to navigate between revisions.

Can I use revisions with custom post types in WordPress?

Yes, you can use revisions with custom post types. However, you need to make sure that the ‘revisions’ feature is enabled for your custom post type.

How can I see who made changes to a post in WordPress?

If you have multiple authors, you can see who made changes to a post by looking at the author name next to each revision in the revisions screen.

Does WordPress revisions affect my website’s performance?

If you have a large number of revisions, it can affect your website’s performance because they are stored in your website’s database. However, you can limit the number of revisions or delete old ones to improve performance.

Can I use a plugin to manage revisions in WordPress?

Yes, there are several plugins available that can help you manage revisions in WordPress. Some popular options include WP Revisions Control, Simple Revisions Delete, and Better Delete Revision. These plugins provide additional features like the ability to delete revisions in bulk, limit the number of revisions, and more.

Craig BucklerCraig Buckler
View Author

Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.

mysqlperformancesitegroundWordPress
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week