Leave a Reply

2 comments

  1. Change your form as suggested:

    function startdate() {
        global $post;
        $custom = get_post_custom($post->ID);
        echo "<label>Startdates</label><br/>";
        for ($i=0; $i<count($custom["startdate"]);$i++) {
            echo "<input type="text" name="startdate[".$i."]" value="".$custom["startdate"][$i]."" />";
        }
    }
    

    You’ll have to remove and reinstate your individual postmeta entries:

    add_action('save_post', 'save_details');
    
    function save_details($post_id) {
        if ($parent_id = wp_is_post_revision($post_id)) $post_id = $parent_id;
    
        if (!empty($_POST['startdate']) && is_array($_POST['startdate'])) {
            delete_post_meta($post_id, 'startdate');
            foreach ($_POST['startdate'] as $startdate) {
                add_post_meta($post_id, 'startdate', $startdate);
            }
        }
    }
    

    Then, of course, you’ll need to add some sort of add/remove mechanism to your metabox form, probably through JS.

  2. As mentioned by t31os try changing your form inputs to use name="startdate[]" then loop through the array saving each value:

    foreach ($_POST["startdate"] as $datevalue) {
        update_post_meta($post->ID, "startdate", $datevalue);
    }