I’ve made a Meta Box for Posts and Pages, and while testing added Attachments too. But, curiously, the hook save_post
does not fire.
$cpts = apply_filters( 'my_filter', array( 'post', 'page' ) ); // Added attachment through filter
foreach( $cpts as $pt )
{
add_meta_box(
'my_metabox_id',
__( 'Meta Box' ),
array( $this, 'my_metabox_callback' ),
$pt,
'side'
);
}
add_action( 'save_post', array( $this, 'save_metabox_data' ) );
Why is that? Isn’t attachment
a Post Type too and hence subject to the same hooks as other post types?
Not really, attachments are still not “full post types”. Manny Flerumond hints this quite well in this thread:
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
: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.