Add Snippet To Project
When you’re constantly updating and revising your WordPress posts, you might have noticed a buildup of post revisions. On one hand, these revisions can be beneficial for backtracking and restoration, but on the other hand, they can pile up, leading to an unnecessary increase in the size of your website’s database, potentially slowing it down. If you want to keep your website running smoothly by limiting the amount of post revisions stored, then this article is for you. We’ll guide you through a step-by-step process of how to cap the number of saved revisions.
function wpturbo_limit_post_revisions( $num ) {
return 5;
}
add_filter( 'wp_revisions_to_keep', 'wpturbo_limit_post_revisions', 10, 2 );
The code snippet above allows us to limit the number of post revisions WordPress saves per post. WordPress, by default, saves an unlimited number of revisions which in time can end in a bloated database.
At the start of the code, we define a function wpturbo_limit_post_revisions($num). In between the curly braces, we set the return value to 5. This indicates that we only want to keep the 5 most recent revisions for each post.
function wpturbo_limit_post_revisions( $num ) {
return 5;
}
With the function defined, we are ready to hook it to the 'wp_revisions_to_keep' filter provided by WordPress. The add_filter function that we are using takes four parameters:
- The name of the filter to hook the function to.
- The function to be hooked (
wpturbo_limit_post_revisionsin our case). - The priority at which our function should run. By setting it to
10, we’re saying that our function should be executed at the default priority. - The number of arguments our function accepts. Our function
wpturbo_limit_post_revisionsaccepts two parameters, so we set it to2.
add_filter( 'wp_revisions_to_keep', 'wpturbo_limit_post_revisions', 10, 2 );
That’s it! When this code runs, WordPress will now only keep the 5 most recent revisions for each post in the database. This helps to reduce clutter and keeps your database running smoothly by lowering the quantity of stored data.
