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.

is there a way to store the input value from multiple custom meta box fields with the same meta_key? I use the following code to store ONE value for the meta_key 'startdate':

function startdate() {
  global $post;
  $custom = get_post_custom($post->ID);
  $startdate = $custom["startdate"][0];
  ?>

<label>Startdate</label><br/>
<input type="text" name="startdate" value="<?php echo $startdate; ?>"/>

<?php }

add_action('save_post', 'save_details');
function save_details(){
  global $post;
  update_post_meta($post->ID, "startdate", $_POST["startdate"]);
}

If i had a second input field, how can i store its value with a different meta_id but the same meta_key (startdate)? Thank you very much!

(If i use the built-in custom field functionality i can save multiple values for the same meta key...)

share|improve this question
    
Yes, just create an array of the data, update your field names to, name="startdate[]" –  t31os Mar 1 '11 at 14:43
    
My Problem is, i have a solution for a custom query that works without arrays. wordpress.stackexchange.com/questions/10328/… I don't know how to do the query with an array. –  Christoph Mar 1 '11 at 14:55
    
what are you trying to get out of the query? –  Bainternet Mar 1 '11 at 15:38
1  
I'm trying to get a chronological list of events. The crux is that each event (custom post) can have multiple dates, added as meta data. @Scribu pointed out how to do a query if the meta data is stored as individual custom fields with the same meta_key wordpress.stackexchange.com/questions/10328/… –  Christoph Mar 1 '11 at 15:59
1  
@t31os: If you ask me, the custom field UI is one of the crappiest bits of UI left in WP. It's for developers, not for users. Hence i can't blame anyone who wants to implement a metabox instead. –  wyrfel Mar 2 '11 at 0:16
show 2 more comments

2 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
Wow, almost there! Two issues i have: (1) When i create a new post (event) i get a warning in edit post area: "Warning: Invalid argument supplied for foreach()". When i enter dates into the form and save the post, the input values get stored correctly as individual postmeta entries (i see them in the db) and the warning disappears. But (2) on reload the meta_values are not set as input form values, so the form inputs appear to be empty. Any ideas how to solve those issues? Thank you very much! –  Christoph Mar 1 '11 at 22:37
    
@Christoph: Sorry, slackerstupidness on my side. For one, wp_is_post_revision() takes the post id as parameter, then, i didn't adjust the value attribute of the inputs properly in the form part, and finally, i now added some basic sanity checks to the update function. See edits. –  wyrfel Mar 2 '11 at 0:03
add comment

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);
}
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.