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.

Here is what I wanna do and I basically would just like some ideas from someone more experienced that would help to get started in the right direction.

I have a custom post type (Exams) that has is supposed to have some custom location meta data.

The idea is that an exam can have one or more locations and I would like to know how to go about this best. The location consists of a name and some more detailed data (basically in a textarea).

I would like to have a single set of fields (name + description) with an add button beneath it, if a user clicks the add button the field is duplicated and one more location can be entered and so on.

The problem here is not how to do the actual duplicating and appending of the fields, I can handle that via jQuery, but how to handle and save the data on the backend. How should I name the fields and how can iterate over them and store them best, etc.

share|improve this question

1 Answer 1

This probably isn't the best way to proceed, but seeing as nobody else answered...

I recently was in the same situation, heres how I did it :

  1. Named the fields so I could access them as an array in the $_POST (ex. name="description[]"), and gave new appended fields the same name.
  2. Using the 'save_post' hook, gathered the data in $_POST
  3. Deleted all previous entries for the custom field using 'delete_post_meta'
  4. Did a For loop on either field and combined each pair into one post meta entry because I had pairing problems when adding them seperately, ex :

    for( $i = 0; $i < count($names); $i++ ){
    
        $meta = $names[$i] . "-=-" . $descriptions[$i]; //combine with a seperator
    
        add_post_meta( $post->ID, "name_description", $meta ); //Save custom field
    
    }
    

Then, when showing the values saved, simply split the field on the seperator you added, and add the paired values into their respective input field.

Of course, I validate the information first, and make sure there aren't empty values and such, but that's the gist of it.

share|improve this answer
    
This sounds a lot like the concept I have been working out in my head, I just figured there is probably a cleaner way of doing it. Thanks for your answer in any case –  ximi Nov 24 '11 at 16:07

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.