Using Auto Draft Posts

There’s no doubt that WordPress uses “Autosave” feature for some pretty valid reasons and it is very useful in certain scenario. Personally though I never liked this feature and find it to be not so useful for me as I am the only one who is and most probably will be writing on my on Blog site. Your case may be different.

In general, every-time you click on “Add New” button from either Posts of Pages section of your site, WordPress automatically creates a record on your database with “auto-draft” status. If you click on Save Draft of Publish button, that record gets retrieved from the database and allows you to edit that record and creates another record on the database for maintaining revisions of the current record.

Here is an example for better understanding. Let’s say the last record or post you published had the id number of 199. As soon as you click on “Add New” button from posts or pages section (even before writing anything), a record gets created on your database with id number of 200 which is your auto draft post. If you write something and click on “Save Draft” or “Publish” button, WordPress will insert any information that you provided to this record (ID 200) and it will be pulled back to your editing window so that you can continue writing your post. However, another record with id number 201 will get created to maintain the post revisions for post that you are currently editing. How many such record gets created depends on number of factors. That’s not my headache here.

Problem is, if you click on “Add New” button and decides not to write anything or may be you decide to write later, it will still create an auto-draft (post_status on your wp_posts table) post and it will reside there with the id number 200. Next time when you click on Add New button and actually write something it will have the id of 201. The record with id number 200 will remain there on your database and it will be deleted by WordPress after 7 days from the time when the record was created. I honestly didn’t like the whole issue and how it works.

What Can We Do About it?

Though you should not think of modifying the core WordPress files systems. You can easily avoid such mess and can actually use pretty much every single record that WordPress creates on your default wp_posts table rather than getting them deleted automatically or maintaining whole bunch of revisions for a single post. First thing that I did is to disable the whole post revision system. I already discussed this issue on one of my earlier post. Please feel free to read the post.

define('WP_POST_REVISIONS', false);
define('AUTOSAVE_INTERVAL', 86400);

Add the snippet mentioned above on your wp-config.php file located at the root directory of your WordPress installation. Notice that I used “false” to totally avoid having any post revision. Also I used 86400 seconds (which is 24 hours) on the snippet in order to prolong the interval time before autosave feature kicks in. The default interval time is 60 seconds. So seriously consider to increase the time much higher than whatever it usually takes to finish a post.

What About Auto Draft Posts?

As I mentioned earlier, posts with auto-draft status on database generally gets deleted by WordPress. There are several ways to stop this. One way to do that is to delete “wp_scheduled_auto_draft_delete” schedule event which I don’t think is the right approach simply because this schedule event was registered by WordPress itself. Second method is simply to update all the “auto-draft” posts status to “draft” so that the schedule event don’t get to find them to begin with. Here is how it can be done.

add_action('wp_logout', function() {
   global $wpdb;
   $draft_posts = $wpdb->get_col("SELECT ID, post_status FROM $wpdb->posts WHERE post_status = 'auto-draft'");
   foreach ($draft_posts as $dp) {
      $data['ID'] = $dp;
      $data['post_status'] = 'draft';
      $where['ID'] = $dp;
      $wpdb->update($wpdb->posts, $data, $where); }
});

Simply place this snippet on your current theme’s functions.php page and update it. What is happening here is that every time you manually log out from the system. This snippet will make a database query to find all the posts that has “auto-draft” post status and change post status to be “draft” for each of them. This way you can re-use them rather than simply getting them deleted from the database all together. What’s important here to remember is that you must not keep your account logged in forever. If you log in to the system and write some posts. Make sure you manually log out from the system once you are done with your job.

Can I Schedule This Task?

Absolutely! Perhaps this is the best approach if you want to automate the whole process. I already discussed in one of my earlier post on scheduling task issue. Please go over that post for better understanding. Scheduling task of changing “auto-draft” posts to “draft” status will allow you not to worry about manually signing out from the system. This task will be performed automatically on a regular interval (daily, weekly, monthly etc.). Since WordPress would delete posts on weekly interval you can set this task on daily basis so that any auto draft post get converted to draft if you are a heavy user of the system.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.