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.

Ok, I've looked through the forum here, but haven't found quite what I'm looking for. I wanted to see if it was possible to add a Metabox to all custom post types without the need to setup an array? Or, maybe just the CPTs created using Custom Post Types UI plugin?

Here's the code I'm testing, but it only shows up on regular Posts.

    add_meta_box( 'my-meta-box-id2', 'Enter your PDF location for your post category below:', 'cd2_meta_box_cb', 'post', 'normal', 'high' );
share|improve this question

1 Answer 1

The 4th parameter for add_meta_box() is the post_type. So if you're calling it with post, it'll only show on WordPress's native posts.

Try this:

add_meta_box(
    'my-meta-box-id2',
    'Enter your PDF location for your post category below:',
    'cd2_meta_box_cb',
    'your_custom_post_type_name',
    'normal',
    'high'
);

If you have multiple post types you'd like to attach this to, this should work:

$post_types = array( 'post_type_1', 'post_type_2', ); // and so forth
foreach( $post_types as $post_type) {
    add_meta_box(
        'my-meta-box-id2',
        'Enter your PDF location for your post category below:',
        'cd2_meta_box_cb',
        $post_type,
        'normal',
        'high'
    );
}

(Whoops -- fixed the code to actually use the $post_type variable.)

Reference

Codex page for add_meta_box()

share|improve this answer
    
Ok, so no way to do it without coding them in functions.php ? –  mtuttle Jul 17 '13 at 15:15
    
Also, am I missing something with this? When I place two of the custom post type names into the array, I'm getting a server error: $post_types = array( 'nemultispecies', 'monkfish', ); // and so forth foreach( $post_types as $post_type) { add_meta_box( 'my-meta-box-id2', 'Enter your PDF location for your post category below:', 'cd2_meta_box_cb', 'your_custom_post_type_name', 'normal', 'high' ); } –  mtuttle Jul 17 '13 at 15:20
    
I fixed the 2nd code sample to actually use the $post_type variable. Is it still giving you an error? It could also be that the first parameter (id) needs to be unique. –  Pat J Jul 17 '13 at 15:36
    
I tried this one and got a blank screen: $post_types = array( 'nemultispecies', 'atlanticseascallop', 'monkfish', 'atlanticherring', 'smallmeshmulti', 'smallmeshmulti', 'atlanticseascallop', 'atlanticseascallop', 'atlanticseascallop', 'atlanticseascallop', ); // and so forth foreach( $post_types as $post_type) { add_meta_box( 'my-meta-box-id2', 'Enter your PDF location for your post category below:', 'cd2_meta_box_cb', $post_type, 'normal', 'high' ); } –  mtuttle Jul 17 '13 at 21:00
    
See the documentation here codex.wordpress.org/Function_Reference/get_post_types to be combined with the answer above. That page is about how to get all registered post types. –  Ari Susanto Sep 2 '13 at 16:19

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.