Home > CMS Tutorial > WordPress > How to Take Control of Page and Post Revisions in WordPress

How to Take Control of Page and Post Revisions in WordPress

Christopher Nolan
Release: 2025-02-10 09:06:09
Original
559 people have browsed it

Managing WordPress Post Revisions: A Comprehensive Guide

How to Take Control of Page and Post Revisions in WordPress

This article is a SiteGround partnership contribution. Thank you for supporting our sponsors.

WordPress's revision system automatically saves a complete copy of each page and post upon saving, allowing you to revert to previous versions and compare changes. While helpful, unlimited revisions can impact performance, especially on larger sites. This guide explores various methods for controlling and managing WordPress revisions.

Limiting Revisions

By default, WordPress stores an unlimited number of revisions. To limit this, you can modify the wp-config.php file. Always back up this file before making any changes.

  • Disable Revisions: Add define('WP_POST_REVISIONS', 0); to completely disable revisions.
  • Limit Revisions: Use define('WP_POST_REVISIONS', 10); to limit to ten revisions. Replace 10 with your desired number.
  • Restore Unlimited Revisions: Remove the line or set the value to -1.

Using Plugins for Revision Control

If direct file editing isn't preferred, several plugins offer revision management. WP Revisions Limit, for example, provides a user-friendly interface for setting revision limits.

Programmatic Revision Control

For more advanced control, the wp_revisions_to_keep filter allows customization within your theme's functions.php file or a plugin. This filter accepts two arguments: the default revision count and the WP_Post object. The following example limits revisions to five for posts of 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;
}
Copy after login

The WordPress REST API also offers options for managing revisions.

Removing Old Revisions

While setting WP_POST_REVISIONS takes immediate effect, it doesn't remove existing revisions. Always back up your database before performing database cleanup.

  • Use a Plugin: Plugins like WP-Optimize offer database optimization, including revision removal. They provide a safe and convenient method.
  • SQL Command (Use with Extreme Caution!): After identifying your WordPress table prefix (e.g., wp_ in wp-config.php), execute this SQL command in a MySQL administration tool like phpMyAdmin:
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';
Copy after login

(Credit to Michael Ambrosio for providing this refined SQL command.)

Frequently Asked Questions

This section answers common questions about WordPress revision control, covering topics such as limiting revision numbers, completely disabling revisions, deleting old revisions, the difference between autosave and revisions, restoring previous versions, comparing revisions, using revisions with custom post types, identifying revision authors, performance impact, and using plugins for revision management. (The original FAQ section is incorporated here but reformatted for better readability and flow.)

Remember to always back up your files and database before making any significant changes to your WordPress installation.

The above is the detailed content of How to Take Control of Page and Post Revisions in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template