I have managed to add a meta box to my custom post type (see code below) Now I need to add another meta box. With this box I want to display / edit a value from another table called wp_project_bids_mitglied. This table contains a row with the post ids and a row with the values (0,1,2) that I would like to edit / display in my backend. How do I have to change the code to achieve this goal? Thanks!

function add_custom_meta_box() {
    'custom_meta_box', // $id
    'Dauer', // $title
    'show_custom_meta_box', // $callback
    'project', // $page
    'normal', // $context
    'high'); // $priority
add_action('add_meta_boxes', 'add_custom_meta_box');

// Field Array
$prefix = 'custom_';
$custom_meta_fields = array(

    'label'=> 'Select Box',
    'desc'  => 'Fotoauftrag Dauer angeben',
    'id'    => $prefix.'dauer',
    'type'  => 'select',
    'options' => array (
        'one' => array (
            'label' => '1-3',
            'value' => 'a'
        'two' => array (
            'label' => '3-6',
            'value' => 'b'
        'three' => array (
            'label' => '6-9',
            'value' => 'c'
        'four' => array (
            'label' => '>9',
            'value' => 'd'

// The Callback
function show_custom_meta_box() {
global $custom_meta_fields, $post;
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

// Begin the field table and loop
echo '<table class="form-table">';
foreach ($custom_meta_fields as $field) {
    // get value of this field if it exists for this post
    $meta = get_post_meta($post->ID, $field['id'], true);
    // begin a table row with
    echo '<tr>
            <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
            switch($field['type']) {
                // case items will go here
                // text

case 'select':
echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
foreach ($field['options'] as $option) {
    echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
echo '</select><br /><span class="description">'.$field['desc'].'</span>';
            } //end switch
    echo '</td></tr>';
} // end foreach
echo '</table>'; // end table

// Save the Data
function save_custom_meta($post_id) {
global $custom_meta_fields;

// verify nonce
if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
    return $post_id;
// check 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;

// loop through fields and save the data
foreach ($custom_meta_fields as $field) {
    $old = get_post_meta($post_id, $field['id'], true);
    $new = $_POST[$field['id']];
    if ($new && $new != $old) {
        update_post_meta($post_id, $field['id'], $new);
    } elseif ('' == $new && $old) {
        delete_post_meta($post_id, $field['id'], $old);
} // end foreach

1 Answer 1


The whole bunch of code is to replicate the process, not to give the actual code. You have to figure out how your process can be meet up with this scenario. I commented on every possible line, please see the inline comments to get proper understanding.

Step #1

First of all, the first portion of your code makes a Custom Meta Box, so use the portion to make your meta box first:

//making the meta box (Note: meta box != custom meta field)
function wpse_add_custom_meta_box_2() {
       'custom_meta_box-2',       // $id
       'Dauer2',                  // $title
       'show_custom_meta_box_2',  // $callback
       'project',                 // $page
       'normal',                  // $context
       'high'                     // $priority
add_action('add_meta_boxes', 'wpse_add_custom_meta_box_2');

Step #2

So your meta box is ready now. Now you have to make some Form fields to get user data, and for that we are using the $callback function what we just declared:

//showing custom form fields
function show_custom_meta_box_2() {
    global $post;

    // Use nonce for verification to secure data sending
    wp_nonce_field( basename( __FILE__ ), 'wpse_our_nonce' );


    <!-- my custom value input -->
    <input type="number" name="wpse_value" value="">


Step #3

On the post save the two fields will post values, now we have to save 'em where we want to save 'em.

//now we are saving the data
function wpse_save_meta_fields( $post_id ) {

  // verify nonce
  if (!isset($_POST['wpse_our_nonce']) || !wp_verify_nonce($_POST['wpse_our_nonce'], basename(__FILE__)))
      return 'nonce not verified';

  // check autosave
  if ( wp_is_post_autosave( $post_id ) )
      return 'autosave';

  //check post revision
  if ( wp_is_post_revision( $post_id ) )
      return 'revision';

  // check permissions
  if ( 'project' == $_POST['post_type'] ) {
      if ( ! current_user_can( 'edit_page', $post_id ) )
          return 'cannot edit page';
      } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
          return 'cannot edit post';

  //so our basic checking is done, now we can grab what we've passed from our newly created form
  $wpse_value = $_POST['wpse_value'];

  //simply we have to save the data now
  global $wpdb;

  $table = $wpdb->base_prefix . 'project_bids_mitglied';

                'col_post_id' => $post_id, //as we are having it by default with this function
                'col_value'   => intval( $wpse_value ) //assuming we are passing numerical value
                '%d', //%s - string, %d - integer, %f - float
                '%d', //%s - string, %d - integer, %f - float

add_action( 'save_post', 'wpse_save_meta_fields' );
add_action( 'new_to_publish', 'wpse_save_meta_fields' );

As you are dealing with custom table, so we are sticking with $wpdb class to securely store necessary data.

Please note, this is not the code what you require, this is the idea and the process how you can now shape your path. Just remember two things:

  1. show_custom_meta_box_2() is the area where your HTML form is, treat this part like a simple HTML form, and
  2. wpse_save_meta_fields() hooked to necessary actions, will save the data of the form when the post is published / saved / updated. Take proper sanitization and validation here.

Hope it helps. Thanks to @toscho for the recent chat. <3

  • Wow - great! I will try it now and provide feedback. Many thanks!!!
    – cgscolonia
    May 1, 2015 at 18:08
  • I found a solution and your hints were very helpfull! Thanks again :)
    – cgscolonia
    May 5, 2015 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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