One metabox for multiple post types

I have this code

function add_custom_meta_box() {
add_meta_box(
    'custom_meta_box', // $id
    'Custom Meta Box', // $title 
    'show_custom_meta_box', // $callback
     'page', // $page    
    'normal', // $context
    'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box');

I want to add more post type like page,post,custom_post_type in this code

Read More
  'page', // $page 

How should I rewrite my code?

Related posts

Leave a Reply

2 comments

  1. Define an array of post types, and register the metabox for each one separately:

    function add_custom_meta_box() {
    
        $post_types = array ( 'post', 'page', 'event' );
    
        foreach( $post_types as $post_type )
        {
            add_meta_box(
                'custom_meta_box', // $id
                'Custom Meta Box', // $title 
                'show_custom_meta_box', // $callback
                 $post_type,
                'normal', // $context
                'high' // $priority
            );
        }
    }
    
  2. If your aim is to add all post types, then you can get the array of post types with:

    $post_types = get_post_types( array('public' => true) );
    

    Or add an argument for WP core post types or custom ones:

    // only WP core post types
    $post_types = get_post_types( array('public' => true, '_builtin' =>‌ true) );
    
    // only custom post types
    $post_types = get_post_types( array('public' => true, '_builtin' =>‌ false) );
    

    Use it like this:

    add_meta_box(
        'custom_meta_box', // $id
        'Custom Meta Box', // $title 
        'show_custom_meta_box', // $callback
         $post_types,
        'normal', // $context
        'high' // $priority
    );