Title: wp_revisions_to_keep
Published: April 25, 2014
Last modified: April 28, 2025

---

# wp_revisions_to_keep( WP_Post $post ): int

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#wp--skip-link--target)

Determines how many revisions to retain for a given post.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#description)󠁿

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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#parameters)󠁿

 `$post`[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)required

The post object.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#return)󠁿

 int The number of revisions to keep.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#source)󠁿

    ```php
    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 '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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/revision.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/revision.php#L798)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/revision.php#L798-L844)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#hooks)󠁿

 [apply_filters( ‘wp_revisions_to_keep’, int $num, WP_Post $post )](https://developer.wordpress.org/reference/hooks/wp_revisions_to_keep/)

Filters the number of revisions to save for the given post.

 [apply_filters( “wp_{$post->post_type}_revisions_to_keep”, int $num, WP_Post $post )](https://developer.wordpress.org/reference/hooks/wp_post-post_type_revisions_to_keep/)

Filters the number of revisions to save for the given post by its post type.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#related)󠁿

| Uses | Description | 
| [post_type_supports()](https://developer.wordpress.org/reference/functions/post_type_supports/)`wp-includes/post.php` |

Checks a post type’s support for a given feature.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

| Used by | Description | 
| [wp_revisions_enabled()](https://developer.wordpress.org/reference/functions/wp_revisions_enabled/)`wp-includes/revision.php` |

Determines whether revisions are enabled for a given post.

  | 
| [wp_save_post_revision()](https://developer.wordpress.org/reference/functions/wp_save_post_revision/)`wp-includes/revision.php` |

Creates a revision for the current version of a post.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.6.0](https://developer.wordpress.org/reference/since/3.6.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/?output_format=md#comment-content-1533)
 2.   [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/#comment-1533)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_revisions_to_keep%2F%23comment-1533)
    Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_revisions_to_keep%2F%23comment-1533)
 4. **Example**
 5. This code notifies a user when the post they are working has reached the limit 
    defined by wp_revisions_to_keep
 6.     ```php
        /**
         * Notify the User When they are editing a post that has reached the limit defined by wp_revisions_to_keep
         */
        function wpdocs_myplugin_admin_notices() {
            global $post;
            $revisions = wp_get_post_revisions( $post->ID );
            if ( isset( $post ) && wp_revisions_to_keep( $post ) <= count( $revisions ) ) { ?>
                <div class="error">
                    <p><?php _e( 'Maximum number of revisions reached. Explain some more to your users here', 'myplugin-text-domain' ); ?></p>
                </div>
            <?php
            }
        }
        add_action( 'admin_notices', 'wpdocs_myplugin_admin_notices' );
        ```
    
 7.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_revisions_to_keep%2F%3Freplytocom%3D1533%23feedback-editor-1533)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_revisions_to_keep%2F)
before being able to contribute a note or feedback.