Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

  'page', // $page 

How should I rewrite my code?

share|improve this question
add comment

1 Answer

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
        );
    }
}
share|improve this answer
    
get_post_types could be used to automate the process. –  brasofilo Nov 24 '12 at 15:19
    
@brasofilo If I remember that correctly, passing NULL as post type would catch all. But have never done that. Unpredictable results. –  toscho Nov 24 '12 at 15:35
    
My first comment should had a question mark... But, just tested this and works fine: $post_types=get_post_types(array('public'=>true,'_builtin'=>false)); array_push($post_types,'post','page'); –  brasofilo Nov 24 '12 at 15:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.