Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
$key = "key";
$meta_boxes = array(
    "item-count" => array(
    "name" => "item-count",
    "type" => "itemcount",
    "std" => "",
    "title" => "How many items?",
    "description" => "Choose item count",
    "options" => array(
        "0" => "None", "1" => "1", "2" => "2", "2" => "2", "3" => "3", "4" => "4"),
    ),
    "item-color" => array(
    "name" => "item-color",
    "type" => "itemcolor",
    "std" => "",
    "title" => "Item Colors",
    "description" => "Choose item colors",
    "options" => array(
        "black" => "Black", "white" => "White", "red" => "Red"),
    ),
);

function create_meta_box() {
global $key;

  if ( function_exists('add_meta_box') ) {
      add_meta_box( 'item-div', 'Item Infos', 'display_meta_box', 'post', 'normal', 'high' );
  }
}
function display_meta_box() {
    global $post, $meta_boxes, $key;

    wp_nonce_field( plugin_basename( __FILE__ ), $key . '_wpnonce', false, true );

    foreach($meta_boxes as $meta_box) {
    $data = get_post_meta($post->ID, $key, true);

    switch ( $meta_box['type'] ) {

    case "itemcount":
    echo '<table style="margin-bottom: 20px;" class="widefat">';
    echo '<thead><tr><th>'.$meta_box['title'].'</th></tr></thead>';
    echo '<tbody style="background:#f9f9f9;"><tr><td>';
    echo'<select name="'.$meta_box['name'].'[]" multiple size="3">';
    function new_in_array( $needle, $haystack )  { if (is_array($haystack)) { return in_array($needle, $haystack); } else { return false; } }
$getcountmeta = get_post_meta( $post->ID, 'key', true );
$getcountmeta2 = $getcountmeta['item-count']; ?>
<?php foreach ($meta_box['options'] as $item_count_key => $item_count_option) { ?>
<option <?php if ( new_in_array($item_count_key,$getcountmeta2)) { ?> selected <?php } ?> value="<?php echo $item_count_key; ?>"><?php echo $item_count_option; ?></option>
<?php }
    echo '</select>';
    echo '<p>'.$meta_box['description'].'</p>';
    echo '</td></tr></tbody></table></div>';
    break;

    case "itemcolor":
    echo '<table style="margin-bottom: 20px;" class="widefat">';
    echo '<thead><tr><th>'.$meta_box['title'].'</th></tr></thead>';
    echo '<tbody style="background:#f9f9f9;"><tr><td>';
    echo'<select name="'.$meta_box['name'].'[]" multiple size="3">';
    function new_in_array2( $needle, $haystack )  { if (is_array($haystack)) { return in_array($needle, $haystack); } else { return false; } }
$getcolormeta = get_post_meta( $post->ID, 'key', true );
$getcolormeta2 = $getcolormeta['item-color']; ?>
<?php foreach ($meta_box['options'] as $item_color_key => $item_color_option) { ?>
<option <?php if ( new_in_array2($item_color_key,$getcolormeta2)) { ?> selected <?php } ?> value="<?php echo $item_color_key; ?>"><?php echo $item_color_option; ?></option>
<?php }
    echo '</select>';
    echo '<p>'.$meta_box['description'].'</p>';
    echo '</td></tr></tbody></table></div>';
    break;
    }
    }
}

function save_meta_box( $post_id ) {
global $post, $meta_boxes, $key;

foreach($meta_boxes as $meta_box) {
$data[ $meta_box[ 'name' ] ] = $_POST[ $meta_box[ 'name' ] ];

}

if ( !wp_verify_nonce( $_POST[ $key . '_wpnonce' ], plugin_basename(__FILE__) ) )
return $post_id;

if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;

update_post_meta( $post_id, $key, $data );
}

The code i pasted above, adds the "key" meta_key to database and its meta_value is

a:2:{s:10:"item-count";a:2:{i:0;s:1:"1";i:1;s:1:"2";}s:10:"item-color";a:2:{i:0;s:5:"black";i:1;s:5:"white";}} //i chose counts 1 and 2, colors black and white

What i want to achieve is, instead of unique "key" meta_key, store those values in seperate meta keys such as

meta_key     item_color
meta_value   a:2:{i:0;s:5:"black";i:1;s:5:"white";}

I have no idea how to do this. Any suggestion?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Your first line reads:

$key = "key";

And your last line reads:

update_post_meta( $post_id, $key, $data );

So, for all I can see here, if you change it to:

$key = "item_color";

You should be good to go.

share|improve this answer
add comment

Your Answer

 
discard

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.