-
-
Save superbiche/fac2c9742b7c1e71a8b3 to your computer and use it in GitHub Desktop.
add_action('save_post', 'my_post_saved'); | |
function my_post_saved($post_id, $post) { | |
if (isset($post->post_status) && 'auto-draft' == $post->post_status) { | |
return; | |
} | |
// Autosave, do nothing | |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { | |
return; | |
} | |
// AJAX? Not used here | |
if (defined('DOING_AJAX') && DOING_AJAX) { | |
return; | |
} | |
// Return if it's a post revision | |
if ( false !== wp_is_post_revision( $post_id ) ) { | |
return; | |
} | |
// Do what you want | |
} |
@jaredatch did you test the code you wrote? I tried it and it doesn't filter out the auto-draft posts. The wp_is_post_autosave($post_id) function tests if the post is a revision (all auto-saves are revisions) and then looks for '-autosave' in the post name. So autosaves and auto-drafts are not the same. Could you please let me know if your code works for you? Thank you.
PS:In this video at 4:41 https://www.youtube.com/watch?v=9GuJi8dYuAs the author uses the following code to successfully filter out auto-drafts (I tried it and it works) but I don't get the logic of it (I asked but didn't get an answer):
if(! (wp_is_post_revision( $post_id )) || wp_is_post_autosave($post_id) ) {
return;
}
This means to me : if it is NOT a revision or if it IS an autosave then return.
@thenoobtester - you are indeed right. not sure what I did differently the first time, or perhaps my test wasn't looked at closely though, but I revisited and what I had mentioned did NOT work. I removed that comment so I don't confuse anyone else. Thanks for the heads up!
@jaredatch thank you for the reply, I was driving myself crazy over this as I just couldn't filter out the auto drafts with that code and couldn't understand why it didn't work in my environment:)
Thank You for sharing! Very helpful.