The problem is probably that the $post_id
parameter passed to via the save_post_$post_type
hook is not the same $post_id
as the original post - it's the $post_id
of the temporary post that's created when you edit a post.
To confirm this:
add_filter( 'pre_post_update', 'wpse_pre_post_update', 10, 3 );
add_filter( 'save_post', 'wpse_save_post', 10, 1 );
function wpse_pre_post_update( $post_id, $post, $update ) {
echo $post_id . '<br>';
}
function wpse_save_post( $post_id ) {
echo $post_id;
wp_die();
}
If you're trying to add a new post, then pre_post_update hook doesn't fire and WP will die before taking you to edit screen. Comment out the wp_die()
line, then click the "Add New" link. Now, uncomment out the wp_die()
and save the post. Two non-identical IDs should be output before WP dies.
The first ID is the ID of the actual post.
The second ID is the ID of the temporary post.
It doesn't really make sense to me why WordPress does it this way, and I have no idea if it's always been like this. But it has this behavior for me running a fresh install of WP 4.7.3.
Unless for some reason you need to update the option after the post is saved, I'd use the pre_post_update
hook instead.
add_filter( 'pre_post_update', 'update_promo_tag_id', 10, 3 );
function update_promo_tag_id( $post_id, $post, $update ) {
if( 'promo' !== $post->post_type ) {
return $post_id;
}
update_field( 'one_time_tag_id', 'updated!', $post_id );
return $post_id;
}