wp_revisions_to_keep()
Determine how many revisions to retain for a given post.
By default, an infinite number of revisions are kept.
The constant WP_POST_REVISIONS can be set in wp-config to specify the limit of revisions to keep.
Used By: wp_save_post_revision()
1 time — 0.0005041 sec (slow) | 50000 times — 0.04072 sec (speed of light) | PHP 7.4.25, WP 5.9
Hooks from the function
Return
Int
. The number of revisions to keep.
Usage
wp_revisions_to_keep( $post );
- $post(WP_Post) (required)
- The post object.
Examples
#1 Find out how many revisions the post may keep
On a clean WordPress installation, find out what the revision limit of the very first post is.
// get the post with ID=1 $post = get_post( 1 ); // Get the revision limit for this post $qty = wp_revisions_to_keep( $post ); // it will print -1, i.e. an infinite number of revisions can be stored print_r( $qty ); //> -1
#2 Enable revisions for a separate post
Suppose we have a post type article
which does not support revisions. But we need to enable revisions for one particular post, for example, with ID 54.
add_filter( 'wp_revisions_to_keep', function( $num, $post ){ if( 54 === $post->ID ){ return -1; } return $num; }, 10, 2 );
Changelog
Since 3.6.0 | Introduced. |