I’m trying to fire some code only when a document type is created using save_post, but $update is always true, even when first publishing it.
I assume this is because there’s an autodraft created first. Is there any way around this?
I’m trying to fire some code only when a document type is created using save_post, but $update is always true, even when first publishing it.
I assume this is because there’s an autodraft created first. Is there any way around this?
So appreciate this is a bit late but I was having the exact same issue, the $update parameter is almost completely useless if you want to check whether it is a new post or not.
The way I got around this was to compare the $post->post_date
with $post->post_modified
. Full code snippet below.
add_action( 'save_post', 'save_post_callback', 10, 3 );
function save_post_callback($post_id, $post, $update) {
$is_new = $post->post_date === $post->post_modified;
if ( $is_new ) {
// first publish
} else {
// an update
}
}
Hope that helps anybody else finding this.