wp_revisions_to_keep( WP_Post $post )

Determine how many revisions to retain for a given post.


Description 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 Parameters

$post

(WP_Post) (Required) The post object.


Top ↑

Return Return

(int) The number of revisions to keep.


Top ↑

Source Source

File: wp-includes/revision.php

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

	if ( true === $num ) {
		$num = -1;
	} else {
		$num = intval( $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.
	 */
	return (int) apply_filters( 'wp_revisions_to_keep', $num, $post );
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.6.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Example

    This code notifies a user when the post they are working has reached the limit defined by wp_revisions_to_keep

    /**
     * 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' );
    

You must log in before being able to contribute a note or feedback.