Limit the number of post/page versions stored in the database without a plugin

True story: If you use a page builder powered theme like Divi then you have way too many versions of the same pages and posts stored in your database. Every time you update the page and check how it looks, you’re saving a version of that post or page to your site and in doing so, pushing the size up and up and up. Though the new front end builder in Divi 3.0 is sure to save you the need to push the update button as often, this can still be a problem.

version-control
  • Google+
  • Facebook
  • Twitter

Though there are several plugins that handle the number of versions saved, if that’s all you’re trying to do then you might find them a little bloated. Luckily there’s a simple filter called ‘wp_revisions_to_keep‘ which makes it easy to limit the maximum number of revisions  on any post or page.

If you want to limit the number of revisions being kept in the database in your Divi install then add this to your child theme’s functions.php:

add_filter( 'wp_revisions_to_keep', 'divi_limit_revisions', 10, 2 );

function divi_limit_revisions( $num ) { 
 $num = 3;
 return $num;
}

Be sure to drop it in between the opening and closing ( <?php … ?> ) tags. If you haven’t got a child theme, you can grab one here. This limits the number of revisions to three, but you can change that number to anything you want.

I hope you find this useful.

Stephen James

SJ is a web developer living in the coastal town of Southsea, England. He is a Divi and WordPress advocate and the founder of Divi Space.

Previous post
Next post

10 Comments

  1. Sandy

    Does this function actually clean up the revisions saved prior to the code being put in place? Or just going forward?

    Reply
  2. Colin

    Nice idea. Cheers

    Reply
  3. ben

    brilliant! thanks!

    Reply
  4. Quintin

    HI, as above, does it clean up previous posts or just the new ones.
    I adds the code as above and it didn’t change the number of revisions – still says 60 on the one page.

    Reply
  5. John Anderson

    Oh wow! added this to functions.php in my child theme. updated my current page which had over 100 revisions and bam back down to the 5 I set.

    Nice one SJ!

    Reply
  6. Fecsó

    Thanks.
    There is a similarly easy solution. You can put in the wp-config.php the following line:

    define(‘WP_POST_REVISIONS’, 5);

    Reply
  7. Wayne

    Could this speed a site up if it’s taking a while to load?

    Reply
    • Cory Jenkins

      Yes it potentially could speed up the site… post revisions can bloat the database if there are too many, which can affect site speed.

  8. Leisa

    woo hoo thanks SJ. nice to see all those revisions gone

    Reply
  9. Karen

    Love this. Thanks.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *


Receive notifications about our new blog posts.


Don’t leave before entering to win an iPad Pro! It’s Free!

You may be too early for our Black Friday specials, but you can still enter our competition! Leave your details below and you could win an iPad Pro!

Great you've entered our giveaway! Only one step remaining! Please check your email and confirm it.