Support

Account

Home Forums Add-ons Repeater Field Moving meta field data with update_field or add_row

Solving

Moving meta field data with update_field or add_row

  • Hi!

    I’m not an expert, I’m sorry if I’m missing something important here.

    I have a Relationship field (actually several of them), that is a list of post IDs. It’s called ‘Sponsors’.

    Now, we need the option to choose if each post is going to be linked or not.

    Then, I have created a new Repeater field called ‘Sponsors v2’. And each row has two fields, ‘organization’ and ‘linked’.

    In order to move the date, I tried it with add_row. But comparing the outcome in the database, it doesn’t match.

    The Relationship field has a value that is an array:

    sponsors = a:2:{i:0;s:3:"310";i:1;s:2:"86";}

    But when manually updating the new Repeater field, it creates several independent values…

    
    sponsors-v2 = 2,
    sponsors-v2_0_organization = 310,
    sponsors-v2_0_linked = 0,
    sponsors-v2_0_organization = 86,
    sponsors-v2_0_linked = 0,
    

    I’m confused because reading the documentation I understand that I should use add_row, but it’s creating an array, instead of the data structure above.

    I ended up doing this in development, and seems to work well:

    
    function science_update_logos () {
    
        $activities = get_posts([
            'post_type' => 'scc_cpt_activity',
            'nopaging' => true,
        ]);
    
        foreach ($activities as $activity) {
    
            $activity_id = $activity->ID;
            
            // Sponsors
            // ====================================
            $sponsors = get_field( 'sponsors', $activity_id );
            $sponsors_amount = intval(count($sponsors));
            update_field('sponsors-v2', $sponsors_amount, $activity_id);
            $index_sponsors = 0;
    
            foreach ($sponsors as $organization_id) {
                $sponsor_organization = 'sponsors-v2_'.$index_sponsors.'_organization';
                $sponsor_linked = 'sponsors-v2_'.$index_sponsors.'_linked';
    
                update_field($sponsor_organization, $organization_id, $activity_id);
                update_field($sponsor_linked, '0', $activity_id);
    
                $index_sponsors++;
            };
    
            // Next field
            // ====================================
            ...
    
        }
    
    };
    
    science_update_logos();
    

    I’m scared I’m going to destroy the production site… It should be applied to different fields and around 200 posts. Am I missing something? Am I doing something dramatic?

    Massive thanks!

  • Do not do your second code.

    Explanation of db columns

    
    sponsors-v2 = 2,                    number or rows in the repeater
    sponsors-v2_0_organization = 310,   row 1 (index 0) organization sub field value
    sponsors-v2_0_linked = 0,           row 1 (index 0) linked sub field value
    sponsors-v2_1_organization = 86,    row 2 (index 1) organization sub field value
    sponsors-v2_1_linked = 0,           row 2 (index 1) linked sub field value
    

    all of these entries are managed automatically by ACF
    You should be using add_row()
    trying to insert values the way you are will break the repeater.

  • Thanks a lot John!

    Could you please point me out the guidelines of using add_row() in my case?

    I literally spent a whole day trying to make it work. That’s why I used the code I shared. I’m glad I asked!

  • https://www.advancedcustomfields.com/resources/add_row/

    You should use the field keys and not field names if you are adding the rows to an empty repeater.

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

You must be logged in to reply to this topic.