save_post not working with attachments

Not really, attachments are still not “full post types”. Manny Flerumond hints this quite well in this thread:

Was thinking about this the past few days: currently any media files
uploaded to a WordPress site defaults to the post status of inherit,
which is a holdover to when media attachments were just that. Media
was attached to a certain post, so it inherited the post’s status.
We are starting to get away from attaching media directly to posts
now and are even making them post like by allowing meta boxes and
taxonomies in recent versions of WP. I think a next logical step is
to give media post statuses other than inherit. I can think of a
number of cases where making an image or file private could be
useful, among other things.


I’ve found the solution in this Stack Overflow post: “save_post” hook not working on post type attachment. We have to use the hook edit_attachment:

add_action( 'edit_attachment', array ( $this, 'save_attachment_mb_data'), 10, 1 );
public function save_attachment_mb_data( $post_id ) {
    // do_our_thing();
}

Note that it takes only one parameter, $post_id, so we cannot reuse the same callback as other post types. Well, unless we drop the second one ($post_object) for regular post types.