Support

Account

Home Forums General Issues Adding rows into a Repeater Field programatically Reply To: Adding rows into a Repeater Field programatically

  • Recently had the same issue as mentioned here. The basic scenario was that I was building a new website using data from the old website with new meta keys. Took me a couple of days to figure out how the repeater field works, so will share here and hopefully it helps someone. I know how frustrating it can be to sort something like this out, so bear with me!

    First and foremost, the data as saved in the DB of the old site were all serialized arrays, so what I did was to export those meta keys/values and temporarily import them to the new site under the original meta_key so that I could be sure I was working with the correct data. Then, using WP_Query I queried this custom meta data, which I assigned to the same post type I was looking to import the data to. I had to then assign each serialized array which the query produced to its own variable, check whether the data I received was actually an array and then iterate over each array key/value using a foreach loop, adding key/value pair to the new database IF a value existed for it.

    An important thing to remember with repeaters is that you HAVE to make sure that the base repeater field numeric key value gets updated in accordance with the amount of repeater field sets your data will have. In other words, if your repeater needs to for example have 4 field sets (4 different sets of data) you HAVE to make sure that the value of your base repeater field key is updated to 4 in the DB, otherwise those additional fieldsets WILL NOT show in the admin screen or any frontend forms you might be trying to render, regardless of whether the additional meta keys/values had been correctly imported into the DB. The base repeater field key value in the db tells it how many repeater fieldsets need to be rendered.

    My code is such (probably not the cleanest but it works!)

    <?php
               
               $update_career = new WP_Query(array(
                   'post_type'          =>      'resume',
                   'posts_per_page'     =>  -1,
               ));
               
               ?>
                
                <?php if($update_career->have_posts()): ?>
                
                    <?php while($update_career->have_posts()): $update_career->the_post(); ?>
                
                    <?php 
                    
                    $base_experience_array = get_post_meta(get_the_ID(), '_candidate_experience', true);  
                    
                    ?>
                
                    <?php 
                    
                    $base_experience_array_0 = $base_experience_array[0];
                    $base_experience_array_1 = $base_experience_array[1];
                    $base_experience_array_2 = $base_experience_array[2];
                    $base_experience_array_3 = $base_experience_array[3];
                    $base_experience_array_4 = $base_experience_array[4];
                    $base_experience_array_5 = $base_experience_array[5];
                    $base_experience_array_6 = $base_experience_array[6];
                    $base_experience_array_7 = $base_experience_array[7];
                    $base_experience_array_8 = $base_experience_array[8];
                    
                    if(is_array($base_experience_array_8)){
                    foreach ($base_experience_array_8 as $key => $value){
                            if($key == 'employer' && $value !== ""){
    
                                add_post_meta(get_the_ID(), 'candidate_career_history_8_employer', $value);
    //initially add base repeater field post meta to ensure the sub-fields are shown
        //                        add_post_meta(get_the_ID(), 'candidate_career_history', 1);
    //after that simply update the base field value to reflect the sub fieldset qty    
                            update_post_meta(get_the_ID(), 'candidate_career_history', 9, 8);
    
                            }elseif ($key == 'job_title' && $value !== "") {
    
                                add_post_meta(get_the_ID(), 'candidate_career_history_8_job_title', $value);
    
                            }elseif ($key == 'notes' && $value !== "") {
    
                                add_post_meta(get_the_ID(), 'candidate_career_history_8_duties', $value);
    
                            }elseif ($key == 'machinery' && $value !== "") {
    
                                add_post_meta(get_the_ID(), 'candidate_career_history_8_machinery', $value);
    
                            }elseif ($key == 'reason' && $value !== "") {
    
                                add_post_meta(get_the_ID(), 'candidate_career_history_8_reason_for_leaving', $value);
    
                            }elseif ($key == 'date' && $value !== "") {
    
                                add_post_meta(get_the_ID(), 'candidate_career_history_8_start_end_date', $value);
    
                            }elseif ($key == 'projects' && $value !== "") {
    
                                add_post_meta(get_the_ID(), 'candidate_career_history_8_projects', $value);
    
                            }
                        }
                    }
                    ?>
    
                    <?php endwhile; ?>
                
                    <?php wp_reset_postdata(); ?>
                
                <?php else: ?>
                
                <p>No information to display.</p>
                
                <?php endif; ?>
    

    To break it down:

    My base repeater field name is ‘candidate_career_history’ with subfields for this repeater being ’employer’, ‘job_history’, ‘duties’, ‘machinery’, ‘reason_for_leaving’, ‘start_end_date’ and ‘projects’. The rest I think is pretty self-explanatory. Like I said, not the cleanest way to do it (I’m still very much learning PHP) but it works and that’s all that counts!

    If anyone has any questions let me know and I’ll try to help when I have a gap. Cheers!