To add a meta box to multiple post types, one add_meta_box
per post type is needed. In this example, instead of duplicating the same code, a foreach
is used. More post types can be added to the array and the meta box will be added in all of them:
add_action( 'add_meta_boxes', 'add_custom_box_wpse_94701' );
function add_custom_box_wpse_94701()
{
// Post types to insert the meta box. Adjust array <-------
foreach( array( 'post', 'portfolio' ) as $pt )
add_meta_box(
'sectionid_wpse_94701',
__( 'Custom parent' ),
'blogroll_box_wpse_94701',
$pt,
'side'
);
}
The example above is taken from this Q&A: List of Posts in a Custom Field
Note that the action hook save_post
takes 2 parameters, $post_id
and $post_object
. The latter contains all the post data (title, content, post type, etc).
There are other checks that can be done inside save_post
. Check this search query for examples of adding meta boxes and saving the post data.