wp_revisions_to_keep()WP 3.6.0

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.

1 time — 0.0005041 sec (slow) | 50000 times — 0.04072 sec (speed of light) | PHP 7.4.25, WP 5.9

Return

Int. The number of revisions to keep.

Usage

wp_revisions_to_keep( $post );
$post(WP_Post) (required)
The post object.

Examples

0

#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
0

#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.

wp_revisions_to_keep() code WP 6.5.2

function wp_revisions_to_keep( $post ) {
	$num = WP_POST_REVISIONS;

	if ( true === $num ) {
		$num = -1;
	} else {
		$num = (int) $num;
	}

	if ( ! post_type_supports( $post->post_type, 'revisions' ) ) {
		$num = 0;
	}

	/**
	 * Filters the number of revisions to save for the given post.
	 *
	 * Overrides the value of WP_POST_REVISIONS.
	 *
	 * @since 3.6.0
	 *
	 * @param int     $num  Number of revisions to store.
	 * @param WP_Post $post Post object.
	 */
	$num = apply_filters( 'wp_revisions_to_keep', $num, $post );

	/**
	 * Filters the number of revisions to save for the given post by its post type.
	 *
	 * Overrides both the value of WP_POST_REVISIONS and the {@see 'wp_revisions_to_keep'} filter.
	 *
	 * The dynamic portion of the hook name, `$post->post_type`, refers to
	 * the post type slug.
	 *
	 * Possible hook names include:
	 *
	 *  - `wp_post_revisions_to_keep`
	 *  - `wp_page_revisions_to_keep`
	 *
	 * @since 5.8.0
	 *
	 * @param int     $num  Number of revisions to store.
	 * @param WP_Post $post Post object.
	 */
	$num = apply_filters( "wp_{$post->post_type}_revisions_to_keep", $num, $post );

	return (int) $num;
}