title: saving custom meta box data in cpt not correct handled
body: <p><strong>SETUP</strong></p>
  
  <p>I wrote a piece of code in which I created a custom post type and a meta box with aditional information for my custom post type called portfolio. This meta box contains 3 fields, a link field, date field and expertise field, which are stored and saved with that custom post type.</p>
  
  <p><strong>PROBLEM</strong></p>
  
  <p>This whole setup works completely fine when I update my portfolio items (no errors), but as soon as i start to add things in other post types, such as pages, messages and other post types i have defined. I have errors and notices appear everywhere, since it tries to assign the meta box to these post items as well. Although I thought i specificly assigned the Meta Box only to the post type called portfolio, i am affraid i made an error somewhere. See the code.</p>
  
  <p>So when I load up a different posttype i get the notice that it has an undefined index </p>
  
  <p><code>Notice: Undefined index: dbt_website-link in /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php on line 161
  </code></p>
  
  <p>When I save it however, I get a whole lot of errors in one time. Besides the notices, i do get also information has been send to the headers error.</p>
  
  <pre><code> Notice: Undefined index: dbt_website-link in /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php on line 161
  
   Notice: Undefined index: dbt_opleverdatum in /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php on line 161
  
   Notice: Undefined index: dbt_text in /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php on line 161
  
   Warning: Cannot modify header information - headers already sent by (output started at /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php:161) in /home/jellyf1q/public_html/test/webfanatics/wp/wp-admin/post.php on line 197
  
   Warning: Cannot modify header information - headers already sent by (output started at /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php:161) in /home/jellyf1q/public_html/test/webfanatics/wp/wp-includes/pluggable.php on line 1228
  </code></pre>
  
  <p><strong>ASSUMPTION</strong></p>
  
  <p>With a bit of deduction i figured this has to do with the meta box trying to be applied to all post types. I yet have no clue how to solve this, since I told it already in the code to only use the post type portfolio as I followed the instructions on <a href="http://www.deluxeblogtips.com/2010/04/how-to-create-meta-box-wordpress-post.html" rel="nofollow">this tutorial</a>. It does actually follow that instruction, since the custom meta box is only applied to the portfolio CPT, yet something happens in the code what makes it seek for the index of the post variable of the metabox fields.</p>
  
  <p><strong>CODE</strong></p>
  
  <p>I supplied the code under here for the metabox. As you see in the metabox array on line 5, i tell it to assign to the post type portfolio </p>
  
  <pre><code>$prefix = 'dbt_';
  $meta_box = array(
      'id' =&gt; 'meta-portfolio',
      'title' =&gt; 'Overige informatie',
      'page' =&gt; 'portfolio',
      'context' =&gt; 'normal',
      'priority' =&gt; 'high',
      'fields' =&gt; array(
          array(
              'name' =&gt; 'Website link',
              'desc' =&gt; 'De link naar het portfolio item bij onze klant',
              'id' =&gt; $prefix . 'website-link',
              'type' =&gt; 'text',
              'std' =&gt; 'http://'
          ),
          array(
              'name' =&gt; 'Opleverdatum',
              'desc' =&gt; 'De opleverdatum van het project',
              'id' =&gt; $prefix . 'opleverdatum',
              'type' =&gt; 'text',
              'std' =&gt; 'dd/mm/yyyy'
          ),
          array(
              'name' =&gt; 'Expertise',
              'desc' =&gt; 'De Expertise die is gebruikt binnen het project',
              'id' =&gt; $prefix . 'text',
              'type' =&gt; 'text',
              'std' =&gt; 'Expertise'
          )
      )
  );
  
  add_action('admin_menu', 'portfolio_add_box');
  
  // Add meta box
  function portfolio_add_box() {
      global $meta_box;
  
      add_meta_box($meta_box['id'], $meta_box['title'], 'portfolio_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
  }
  
  
  // Callback function to show fields in meta box
  function portfolio_show_box() {
      global $meta_box, $post;
  
      // Use nonce for verification
      echo '&lt;input type="hidden" name="portfolio_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" /&gt;';
  
      echo '&lt;table class="form-table"&gt;';
  
      foreach ($meta_box['fields'] as $field) {
          // get current post meta data
          $meta = get_post_meta($post-&gt;ID, $field['id'], true);
  
          echo '&lt;tr&gt;',
                  '&lt;th style="width:20%"&gt;&lt;label for="', $field['id'], '"&gt;', $field['name'], '&lt;/label&gt;&lt;/th&gt;',
                  '&lt;td&gt;';
          switch ($field['type']) {
              case 'text':
                  echo '&lt;input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" /&gt;', '&lt;br /&gt;', $field['desc'];
                  break;
              case 'textarea':
                  echo '&lt;textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%"&gt;', $meta ? $meta : $field['std'], '&lt;/textarea&gt;', '&lt;br /&gt;', $field['desc'];
                  break;
              case 'select':
                  echo '&lt;select name="', $field['id'], '" id="', $field['id'], '"&gt;';
                  foreach ($field['options'] as $option) {
                      echo '&lt;option ', $meta == $option ? ' selected="selected"' : '', '&gt;', $option, '&lt;/option&gt;';
                  }
                  echo '&lt;/select&gt;';
                  break;
              case 'radio':
                  foreach ($field['options'] as $option) {
                      echo '&lt;input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' /&gt;', $option['name'];
                  }
                  break;
              case 'checkbox':
                  echo '&lt;input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' /&gt;';
                  break;
          }
          echo     '&lt;/td&gt;&lt;td&gt;',
              '&lt;/td&gt;&lt;/tr&gt;';
      }
  
      echo '&lt;/table&gt;';
  }
  
  add_action('save_post', 'portfolio_save_data');
  
  // Save data from meta box
  function portfolio_save_data($post_id) {
      global $meta_box;
  
      // verify nonce
      if (isset($_POST['portfolio_meta_box_nonce']) &amp;&amp; !wp_verify_nonce($_POST['portfolio_meta_box_nonce'], basename(__FILE__))) {
          return $post_id;
      }
  
      // check autosave
      if (defined('DOING_AUTOSAVE') &amp;&amp; DOING_AUTOSAVE) {
          return $post_id;
      }
  
      // check permissions
      if ('page' == $_POST['post_type']) {
          if (!current_user_can('edit_page', $post_id)) {
              return $post_id;
          }
      } elseif (!current_user_can('edit_post', $post_id)) {
          return $post_id;
      }
  
      foreach ($meta_box['fields'] as $field) {
          $old = get_post_meta($post_id, $field['id'], true);
          $new = $_POST[$field['id']];
  
          if ($new &amp;&amp; $new != $old) {
              update_post_meta($post_id, $field['id'], $new);
          } elseif ('' == $new &amp;&amp; $old) {
              delete_post_meta($post_id, $field['id'], $old);
          }
      }
  }       
  
  ?&gt;
  </code></pre>
  
  <p>the code for the custom post type is as following.</p>
  
  <pre><code>add_action('init', 'portfolio_register');
  function portfolio_register() {
  
      $labels = array(
          'name' =&gt; _x('portfolio', 'post type general name'),
          'singular_name' =&gt; _x('portfolio', 'post type singular name'),
          'add_new' =&gt; _x('Nieuw', 'portfolio'),
          'add_new_item' =&gt; __('Nieuw portfolio'),
          'edit_item' =&gt; __('Edit portfolio'),
          'new_item' =&gt; __('Nieuw portfolio'),
          'view_item' =&gt; __('View portfolio'),
          'search_items' =&gt; __('Search portfolio'),
          'not_found' =&gt;  __('Nothing found'),
          'not_found_in_trash' =&gt; __('Nothing found in Trash'),
          'parent_item_colon' =&gt; ''
      );
  
      $args = array(
          'labels' =&gt; $labels,
          'public' =&gt; true,
          'has_archive' =&gt; true,
          'publicly_queryable' =&gt; true,
          'show_ui' =&gt; true,
          'query_var' =&gt; true,
          'menu_icon' =&gt; null,
          'rewrite' =&gt; array(
              'slug' =&gt; 'portfolio', 
              'with_front' =&gt; false,
              'hierarchical' =&gt; true),
          'capability_type' =&gt; 'post',
          'hierarchical' =&gt; false,
          'menu_position' =&gt; null,
          'supports' =&gt; array('title','editor','thumbnail'),
          'menu_icon' =&gt; 'dashicons-format-image',
  
            'taxonomies' =&gt; array('post_tag')
      );
  
      register_post_type( 'portfolio' , $args );
  }
  </code></pre>
  
  <p>Line 161 is refer to this piece of code in the meta box. </p>
  
  <pre><code>    foreach ($meta_box['fields'] as $field) {
          $old = get_post_meta($post_id, $field['id'], true);
          $new = $_POST[$field['id']];
  
          if ($new &amp;&amp; $new != $old) {
              update_post_meta($post_id, $field['id'], $new);
          } elseif ('' == $new &amp;&amp; $old) {
              delete_post_meta($post_id, $field['id'], $old);
          }
      }
  </code></pre>