4

I've been trying to find a filter or settings in WordPress to control the Autosave behavior, preferably by post type. When querying the database, I see our auto-drafts only go back one week, is there something in WordPress that controls this behavior? I'd like to be able to have auto-draft kept 90 days or more for some post types if possible?

I read the following article regarding Autosave, perhaps I'm misunderstanding, but my experience is when I create a new post, the record is in auto-draft status and another is created when I refresh post-new.php. This article seems to imply only one Autosave per user...

https://codex.wordpress.org/Revisions

Perhaps Autosave and saving as auto-draft are not related. I'm more interested in how to maintain the auto-draft records longer which may have nothing to do with Autosave. Thanks for any guidance!

1 Answer 1

2

Autosave:

Autosave is simply about saving your posts automatically in the background while you are editing. So it's different from revisions and for every post there will be only one autosave per user. This is from the document:

There is only ever a maximum of one autosave per user for any given post. New autosaves overwrite old autosaves.

You can manipulate how soon the autosave will happen by defining AUTOSAVE_INTERVAL constant in wp-config.php file. For example, to autosave posts / pages you're editing every 30 seconds:

define('AUTOSAVE_INTERVAL', 30 );

This is useful in case your net connection is lost or computer turns off accidentally before you could save the changes. With autosave interval set to 30 seconds, you'll only lose maximum 30 seconds worth of edit.

Revisions:

Revision is about deciding how many changes to your post you want to keep. So if revision is turned on and you save a post 24 times, then WordPress will keep the 24th version as your original post and other 23 versions as revisions.

Note: WordPress doesn't keep autosaved versions as revisions. Only the saves you've made intentionally (by clicking save or publish) are kept as revisions.

By default, WordPress keeps infinite number of revisions if revision is activated. So unless you specify, WordPress doesn't delete any revision automatically. However, this behaviour can be altered using WP_POST_REVISIONS constant or wp_revisions_to_keep filter.

For example, to keep the last 22 revisions only, you may set the following in the wp-config.php file:

define( 'WP_POST_REVISIONS', 22 );

or using the following CODE in your theme's functions.php file or in a custom plugin:

add_filter( 'wp_revisions_to_keep', 'wpse257846_num_revisions_to_keep', 10, 2 );
function wpse257846_num_revisions_to_keep( $num, $post ) {
    // you may use the $post variable to manipulate this number as needed
    if( 'my_custom_post' == $post->post_type ) {
        // for 'my_custom_post' post type, keep last 100 revisions
        // this is only about how many revisions to keep,
        // not about how old they are
        // to decide how old revision you want to keep,
        // you need to make more complex query about date here 
        return 100;
    }
    if( 'my_most_important_post' == $post->post_type ) {
        // returning -1 means WordPress will never delete revisions
        // for 'my_most_important_post' post type
        return -1;
    }
    // if for all other post types we want to keep just the last 22 revisions
    return 22;
}
1
  • Thanks for that clarification, very helpful! But as far as the new post reaching auto-draft and never completed, is it normal that I do not find any of these auto-draft posts over a week old? Can I extend that to keep abandoned posts longer for a specific post type? Commented Feb 24, 2017 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.