wp_revisions_to_keep()WP 3.6.0

Defines how many of the latest revisions (changes) should be stored in the database for a specific post.

This is a wrapper for:

The value of the constant is passed to the first hook, then to the second, and the resulting int (result) is returned. That is, the function relies on the value of the constant WP_POST_REVISIONS and allows changing the result through hooks.

The constant WP_POST_REVISIONS is set in the wp-config.php file.

WP_POST_REVISIONS can be equal to:

  • true (by default) - an infinite number of revisions are saved.

    Default value of the constant is defined in the file /wp-includes/default-constants.php.

  • false or 0 - revisions will be disabled.

  • integer - how many revisions to keep. If 5 is specified, then the last 5 changes of the post will be saved.

Revision support is enabled when registering the post type register_post_type(). Or separately through the function:

post_type_supports( 'post_type', 'revisions' )

If the post type has revision support disabled, the value of the constant WP_POST_REVISIONS is reset. However, even if revisions are disabled for the post type, they can still be enabled through hooks, for example for a specific post. See example 2.

There is also a function wp_revisions_enabled() - checks if revisions are enabled for the post.

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

Returns

Int. The number of revisions that need to be stored for the specified post.

  • integer - how many revisions to keep in the DB.
  • -1 - keep all revisions in the DB.
  • 0 - do not save revisions at all.

Usage

wp_revisions_to_keep( $post );
$post(WP_Post) (required)
Object of the post.

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

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;
}