Add meta box to multiple post types

  1. ckruse


    When adding custom meta boxes to post types, there seems to be no way to include the custom box on mulitple post types. For example, If I want to create a custom meta box which allows the admin to check a box indicating that a specific post/page/custom-post-type is featured. Currently, I have to do something like this:

    add_meta_box('featured-meta', 'Feature Me?', 'custom_meta_featured', 'post', 'side');
    add_meta_box('featured-meta', 'Feature Me?', 'custom_meta_featured', 'page', 'side');
    add_meta_box('featured-meta', 'Feature Me?', 'custom_meta_featured', 'custom-type', 'side');

    Why not allow the use of an array so that instead of the above code, all you need is:

    add_meta_box('featured-meta', 'Feature Me?', 'custom_meta_featured', array('post', 'page', 'custom-type'), 'side');

    Posted: 7 years ago #
  2. derekshirk


    I just had the same question and came looking for this in the codex. Is there a solution for this? I would also like to assign a custom video meta box to both a custom post type and a page.

    Posted: 6 years ago #
  3. ckruse


    Honestly, I never found a solution for this. I just used an array of post types and wrote my own function. Something along the lines of:

    $types = array( 'post', 'page', 'custom-type' );
    foreach( $types as $type ) {
          add_meta_box('featured-meta', 'Feature Me?', 'custom_meta_featured', $type, 'side');

    Obviously, this could be revised to allow for more functionality.

    Posted: 6 years ago #
  4. amabil

    Well, actually there's no point to call add_meta_box multiple times, because at the time when add_meta_box are called, the post type is passed by add_meta_boxes hook.

    Posted: 6 years ago #
  5. amabil

    add_action( 'add_meta_boxes', 'my_add_custom_box' );
    function my_add_custom_box($postType) {
    	$types = array('type1', 'type2', 'type3');
    	if(in_array($postType, $types)){
    				__( 'Title', 'myplugin_textdomain' ),

    Posted: 6 years ago #
  6. jmolina

    Thanks amabil, it works like a charm!

    Posted: 5 years ago #
  7. This is really cool, I am trying to do this, but what if I want to add a unique meta box to each custom post type.

    For example I want to create a custom post type relationship with 3 post types.

    CPT - Recipe
    meta box - Ingredient_recipe_relationship, add multiple ingredients)
    CPT - Ingredient
    meta box - Ingredient_recipe_relationship, add multiple ingredients)
    meta box - Suppliers_Ingredient_relationship, add multiple suppliers)
    CPT - Suppliers
    meta box - Suppliers_Ingredient_relationship, add multiple suppliers)

    I'm told this type of thing doesn't really exist in the WordPress core and has to be custom built by creating a custom table to store relationship data.

    Any thoughts? i just wrote my first custom post type yesterday, am i over my head?

    Posted: 4 years ago #
  8. Ipstenu (Mika Epstein)
    Lead Plugin Wrangler

    Please don't ask support questions in this forum.

    Use the support forums for that: https://wordpress.org/support/

    Posted: 3 years ago #

RSS feed for this topic


You must log in to post.

  • Rating

    12 Votes
  • Status

    Sorry, not right now