Support

Account

Home Forums General Issues Adding rows into a Repeater Field programatically

Solving

Adding rows into a Repeater Field programatically

  • As a note, I am just bulk importing into a single post.

    I currently have a CSV with over 500 values. I want to add a bunch of new rows with the data, and was wondering if there’s a way to programmatically be able to do this?

    I created a Java program to handle the creation of code I need. See below.

    Here’s my code:

    Main.java

    public static void main(String[] args) throws FileNotFoundException, IOException {
            CSVReader reader = new CSVReader(new FileReader("data.csv"));
            String [] nextLine;
            int count = 0;
            while ((nextLine = reader.readNext()) != null) {
                // nextLine[] is an array of values from the line
                Handler data = new Handler(nextLine, count);
                appendToFile(data, "results.txt"); // adds result to text file
                count++;
            }
        }
    

    Handler.java

    Private String lat, long;
    					
    public Handler(String[] line, int count) {
    	if (line != null) {
    		for (int i = 0; i < line.length; i++) {
    			if (line[i] != null) {
    				switch(i) {
    					case 1: this.lat = line[0]; break;
    					case 2: this.long = line[1]; break;
    					default: break;
    				}
    			} else {
    				line[i] = "";
    			}
    		}
    	}
    }
    
    Public String toString() {
    	String repeater = "latlng_repeater"; //repeater field name
    	String value_lat = "latlng_repeater_item_lat"; // sub field name
    	String value_long = "latlng_repeater_item_long"; // sub field name
    
    	String repeater_field_key = "field_53cf18c22ee37; // repeater field key
    	String value_lat_field_key = "field_53cf19022ee3b"; // lat field key
    	String value_lat_field_key = "field_53d09ad6527ac"; // long field key
    
    	String post_id = "248";
    
    	String result = ""; // create code for insertion
    	return result;
    }
    
  • Inserting repeater fields into the DB is something I’ve just needed to do myself. There are a lot of fields to deal with.

    Each repeater field contains the number of rows of sub-fields.
    Each sub-field is actually named {repeater-name}_{count}_{subfield-name}

    So you need to insert the number of rows into “latlng_repeater”
    then for each row, say you have 2, you insert the values into:

    • latlng_repeater = 2
    • latlng_repeater_0_latlng_repeater_item_lat = lat value 1
    • latlng_repeater_0_latlng_repeater_item_long = long value 1
    • latlng_repeater_1_latlng_repeater_item_lat = lat value 2
    • latlng_repeater_1_latlng_repeater_item_long = long value 2

    To top this off, each of these fields has a corresponding field that starts with an _ whose value is the ACF field key. I had cases when working on my import where I needed to insert these values to get the field values to show on the front end without needing to open and save every post in the admin. I also has some cases where the imported values did not even appear in the admin without inserting these.

    • _latlng_repeater = field_53cf18c22ee37
    • _latlng_repeater_0_latlng_repeater_item_lat = field_53cf19022ee3b
    • _latlng_repeater_0_latlng_repeater_item_long = field_53d09ad6527ac
    • _latlng_repeater_1_latlng_repeater_item_lat = field_53cf19022ee3b
    • _latlng_repeater_1_latlng_repeater_item_long = field_53d09ad6527ac

    You really need to add some posts with all of the fields and then dig around in the database to figure out what you need to insert.

    Hope that helps.

    ~JH

  • 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!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.