WordPress.org

Ideas

Add meta box to multiple post types

  1. ckruse
    Member

    12345

    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: 4 years ago #
  2. derekshirk
    Member

    12345

    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: 3 years ago #
  3. ckruse
    Member

    12345

    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: 3 years ago #
  4. amabil
    Member

    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: 3 years ago #
  5. amabil
    Member


    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)){
    		add_meta_box(
    				'myid',
    				__( 'Title', 'myplugin_textdomain' ),
    				'callback',
    				$postType
    		);
    	}
    }

    Posted: 3 years ago #
  6. jmolina
    Member

    Thanks amabil, it works like a charm!

    Posted: 2 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: 10 months ago #
  8. Ipstenu (Mika Epstein)
    Administrator

    Please don't ask support questions in this forum.

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

    Posted: 9 months ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    10 Votes
  • Status

    Sorry, not right now