Support

Account

Home Forums Front-end Issues ACF repeater fields not showing up before manual update Reply To: ACF repeater fields not showing up before manual update

  • I have developed a plugin which fetches posts from another websites API. I have two post types on my local site: post1 and post2
    Plugin compares post1 to post2 and syncs only the missing post2 posts from the API.

    So, a new post is generated:

    function insert_local_post( $local_post ) { // insert new post
    
        $post_data = array(
            'post_type'      => 'post2',
            'post_status'    => 'private',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_author'    => 1,
            'post_name'      => $local_post->post_name,
            'post_title'     => $local_post->post_title,
            'post_date'      => $local_post->post_date,
            'post_date_gmt'  => $local_post->post_date_gmt,
            'post_content'    => '',
        );
    
        $new_post = wp_insert_post( $post_data );
    
        if ( $new_post ) {
    
            update_field( 'unique_identifier', get_field( $this->local_post_identifier, $local_post->ID ), $new_post );
    
        }
        return $new_post;
    }

    After creating the post, post status gets changed to ‘publish’ and updating the fields is called:

    $publish = array( 'ID' => $ID, 'post_status' => 'publish' );
                wp_update_post($publish);
    
                $this->update_local_fields( 'fields1', $fields, $ID);
                $this->update_local_fields( 'fields2', $fields, $ID);
                $this->update_local_fields( 'fields3', $fields, $ID);

    For fields I have a field conf file with a hierarchical set of extra field names. Same hierarchical structure as the fields coming from the API.

    Function for updating the fields:

    function update_local_fields( $field_group_name, $fields, $ID ) {
    
        foreach ( $this->field_conf[$field_group_name] as $field_key => $field_name ) {
    
            if ( ! is_numeric( $field_key ) ) { 
                $this->delete_local_repeater_fields( $field_key, $ID );
                $this->insert_local_repeater_fields( $field_key, $ID, $fields->$field_key, $field_group_name );
                continue;
            }
    
            if ( $fields->$field_name and
                ( $field_name == 'taxonomy1' or
                  $field_name == 'taxonomy2' or
                  $field_name == 'taxonomy3' ) ) { 
    
                $fields->$field_name = $this->link_to_local_taxonomy_terms( $fields->$field_name );
    
            }
    
            update_field( acf_get_field($field_name)['key'], $fields->$field_name, $ID );
    
        }
        return;
    }

    The main chunk of code for creating the rows and subrows for repeater fields:

    function insert_local_repeater_fields( $repeater_row_name, $ID, $fields, $field_group_name ) {
    
            $local_repeater_name = $repeater_row_name;
    
            if ( $fields ) {
    
                for ( $i = 0; $i < sizeof($fields); $i++ ) {
    
                    $row_data = array();
    
                    foreach ( $this->field_conf[$field_group_name][$repeater_row_name] as $row_field ) {
    
                        if ( ! is_array($row_field) ) {
                            $local_row_field = $row_field;
    
                            if ( $fields[$i]->$row_field and $row_field == 'location' ) {
                                $fields[$i]->$row_field = $this->link_to_local_taxonomy_terms( $fields[$i]->$row_field );
                            }
                            $row_data[$local_row_field] = $fields[$i]->$row_field;
                        }
                    }
    
                    add_row(acf_get_field($local_repeater_name)['key'], $row_data, $ID);
    
                    if ( $fields[$i]->cities ) {
                        foreach ( $fields[$i]->cities as $city) {
    
                            $sub_row_data = array();
                            foreach ( $this->field_conf[$field_group_name][$repeater_row_name]['cities'] as $sub_row_field ) {
    
                                $local_sub_row_field = $sub_row_field;
    
                                if ( $city->$sub_row_field and $sub_row_field == 'game' ) {
                                    $city->$sub_row_field = $this->link_to_local_taxonomy_terms( $city->$sub_row_field );
                       
                                }
                                $sub_row_data[$local_sub_row_field] = $city->$sub_row_field;
                          
                            }
                            add_sub_row( array(acf_get_field($local_repeater_name)['key'], ($i + 1), 'cities'), $sub_row_data, $ID );
    
                        }
                    }
    
                }
            }
        }

    The problem is that the fields only show up when I manually update the post and run the sync again. I have tried a million things: using keys instead of fields etc, but still no luck.

    Has anyone encountered the same problem and point me to a potential solution?

    Many cheers!