WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working with a recipes blog and I use custom meta boxes.

$food_box = array(
    'id' => 'foodbox',
    'title' => __('Ingredients details','splus'),
    'page' => 'recipes',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(),);
$arr = array ( 'name' => '-', 'desc' => '-', 'id' => '-','type' => 'text', 'std' => '');
add_action('admin_menu', 'mytheme_add_box');  // Add meta box

function mytheme_add_box() {
    global $meta_box;
    global $food_box;
    global $test_box;

Acoording to recipe ingredients are devided to sets of food and the same food item can be present 2 time in one recipe (for example, 200gr sugar for dough and 50 gr for stuff). To combine two taxonomies (food items and food sets) I created custom table and I add fileds $food_box['fields'][] = $arr; from this table by next code:

foreach ( $terms as $term ) { // show tabs content for foodset
    $s = ($i==1) ? ' ': ' style="display: none;"';
    echo '<div id="set-'.$term->term_id .'" class="tabs-panel"' . $s . '>';
        echo '<ul id="set2-checklist" class="categorychecklist form-no-clear" >';
            echo '<table id="food_data_table_header" class="form-table" border="0" align="left">';
            echo '<tr>';   //show header
                echo '<td style="width: 50%" >  <span>FoodSet</span>        </td>';
                echo '<td style="width: 50%" >  <span>FoodId</span>     </td>';
                echo '<td style="width:100%" >  <span>Description</span>    </td>';
                echo '<td style="width: 50%" >  <span>FoodUnit</span>       </td>';
                echo '<td style="width: 50%" >  <span>Quantity</span>       </td>';
                echo '<td style="width: 50%" >  <span>Order</span>      </td>';
                echo '<td style="width:100%" >  <span>Comment</span>        </td>';
            echo '</tr>';
            foreach ( $foods as $food ) {       //show data
                $food_row = $wpdb->get_row($wpdb->prepare(
                    "SELECT rf.post_id, rf.foodset_id, rf.food_id, tt1.description, rf.food_unit, rf.quantity, rf.food_order, rf.food_comment
                       FROM $wpdb->term_taxonomy tt1 JOIN $wpdb->recipefood rf ON rf.food_id = tt1.term_id
                      WHERE (rf.post_id=".$post_id.") AND (rf.foodset_id=".$term->term_id.") AND (rf.food_id=".$food->term_id.")"));
                $food_set = $term->term_id;
                $food_id = $food->term_id;
                $id = $food_set . '-' . $food_id;
                $food_name = $food_row->description;

    echo '<tr>';
    echo '<td>',$food_set,'</td>';
    echo '<td>',$food_id,'</td>';
    echo '<td>',$food_name,'</td>';
    $fls = array ('food_unit','quantity','food_order','food_comment');
    foreach ($fls as $fl) {
        switch ($fl) {
        case 'food_unit':
        $def_val = $def_unit; break;
        case 'quantity':
        $def_val = $def_qty; break;
        case 'food_order':
        $def_val = '1'; break;
        case 'food_comment':
        $def_val = 'comment'; break;
    $meta = ($food_row->$fl == '') ? $def_val : $food_row->$fl;
    $arr = array ( 'name' => $fl.'-'. $id, 'desc' => $fl.'-'. $id, 'id' => $fl.'-'. $id,'type' => 'text', 'std' => $meta);
    $food_box['fields'][] = $arr; //add new field to array
    echo '<td>','<input type="text" name="'.$fl.'-'.$id. '" id="'.$fl.'-'.$id.'" value="',$meta , '" style="width: 50%"/>', '</td>';
    // $meta ? $meta : $food_box[$fl.'-'.$id]['std']
echo '</tr>';

But unfortunately I can not save my updated data as $_POST['quantity-708-481'] is empty and not defined in

add_action('save_post', 'mytheme_save_data');   // Save recipe post from meta box
function mytheme_save_data($post_id) {
        global $meta_box, $food_box;
        $wpdb->update( $wpdb->recipefood,
        array( //set
            'food_comment' =>  $_POST['quantity-708-481']
        ), array( //where
            'post_id' => '1',
            'foodset_id' => '822',
            'food_id' => '484'

In other words, the field is added in meta box but not visible during saving. Any suggestion are more than welcomed.

share|improve this question
All those $fl.'-'. $id are quite confusing... Compare the HTML with the output of var_dump($_POST);die(); inside the function mytheme_save_data. I'm voting to close as too-localized as this looks too much to a wrong var names problem and debugging all this is a task for you. – brasofilo Apr 12 '13 at 22:18
If you're not hard-coding the id when you output, you shouldn't hard-code it when you're saving as well. – montrealist Apr 12 '13 at 22:18
I added code to show fields 'quantity-708-481', 'food_comment-708-481' etc. – Petro Apr 13 '13 at 22:51

$fl is 'quantity', but from your code snippet it is not clear what $id is set to. I think it is something like 481 or 708. So name="'.$fl.'-'.$id. '" will result in quantity-481 or quantity-708 and not quantity-708-481.

Check the HTML of your metabox if the name is what you expect (use something like FireBug in FireFox or the Developer Tools in Chrome).

share|improve this answer
I have added more code before to clarify that quantity-708-481 or quantity-822-484 is created right. More over, when I echo $field['std'] for $field['id'] == 'quantity-822-484' in the meta box after initialization, I receive correct data from database. So, as I understand fields of meta box are added and I filled. echo count ($food_box['fields']); shows correct number during editing but wrong after saving. – Petro Apr 13 '13 at 22:48
Did you use the Developer Tools in Chrome or FireBug in FireFox? When not, why? There is a function to inspect the sended POST and GET data. Another way to inspect what was send, add die( var_dump( $_POST, $_GET, $_REQUEST ) ); as first line in mytheme_save_data(). This will stop the saving and displaying what was sended. But my best guess is, your fomr fields are not inside the for element. Wrap your form fields with a <form> tag and set the method to POST. – Ralf912 Apr 14 '13 at 12:11
Ralf912, thanks for yourinput. I will try the first suggested method. The second is not clear for me - could you give more info about "method to POST"? – Petro Apr 15 '13 at 12:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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