0

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' );
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()

  • 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. – KeepMove Sep 2 '13 at 16:19
0

You can use this.If we want to add meta box to more than one posttypes then we have to use it inside array.We don't need foreach loop for get this.Just use post type array inside meta box.

add_meta_box(
    'my-meta-box-id2',
    'Enter your PDF location for your post category below:',
    'cd2_meta_box_cb',
    array('custom-posttype-1','custom-posttype-2','custom-posttype-3'),
    'normal',
    'high'
);
  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Mar 1 '17 at 6:45
-1

According to the docs: https://developer.wordpress.org/reference/functions/add_meta_box/ The 4th argument of add_meta_box function can accept arrays too.

$screen

(string|array|WP_Screen) (Optional) The screen or screens on which to show the box (such as a post type, 'link', or 'comment'). Accepts a single screen ID, WP_Screen object, or array of screen IDs. Default is the current screen.

Default value: null

So you can either create an array with your preferred post types:

$post_types = array( 'cpt1', 'cpt2', ... );
add_meta_box( 'my-meta-box', 'My Meta Box','display_my_meta_box', $post_types);

Or simply use the function get_post_types() to get the name of all post types:

$post_types = get_post_types();
add_meta_box( 'my-meta-box', 'My Meta Box','display_my_meta_box', $post_types);

https://codex.wordpress.org/Function_Reference/get_post_types

As it's said in the docs, you can set get_post_types( array( '_builtin' => false ) ); to ignore the wp default post types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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