Advanced Custom Fields – Updating Post object via ACF breaks front-end

I’ve set up a working WP Insert Post, which loops through my API calls.

i am then updating my acf post object field which is successful but… i am facing a problem when i update my acf post object field through this insert post-call, this website breaks the front end.

it doesn’t happen when i use text or select acf field, Only Post object.

I’m wondering if anyone else has encountered this issue, or if there is anything I can do to resolve it.

appreciate your help!

my code below:

foreach($AllStaff[0] as $staff) {

        $staff_slug = sanitize_title($staff->fields->Title . '-' . $staff->id);
        $staff_title = $staff->fields->Title;
        $staff_active = $staff->fields->Active;
        $store = $staff->fields->field_3;
        //Gets location object from custom post type
        foreach($locations as $location) {
            if($store === $location->post_title) {
                $locationObject = get_post( $location->ID );
            }
        };
        $positions = $staff->fields->field_2;
        $specialties = $staff->fields->Specialties;
        $bio = $staff->fields->field_1;
        //Updated Date/Time
        $updatedISO = $staff->lastModifiedDateTime;
        //Image
        $ImageJSON = $staff->fields->Image;
        $ImageArray = json_decode($ImageJSON);
        $ImageUrl = $ImageArray->serverUrl . $ImageArray->serverRelativeUrl;

        $existing_staff = get_page_by_path($staff_slug, 'OBJECT', 'staff');

        remove_action('wp_ajax_get_staff_from_sharepoint', 'get_staff_from_sharepoint');

        if( ! empty($staff_active) ) {
            
            $inserted_staff = wp_insert_post([
                'post_name' => $staff_slug,
                'post_title' => $staff_title,
                'post_content' => $bio,
                'post_type' => 'staff',
                'post_status' => 'publish'
            ]);

            if(is_wp_error($inserted_staff || $inserted_staff === 0)) {
                continue;
            };

            $staff_fields = [
                //Store
                'field_5de70e86a0c61' => $locationObject,
                //Postion
                'field_5de848c06ba3b' => $positions,
                //Specialties - select
                'field_5de8a44c6d194' => $specialties,
                //Updated
                'field_633bf606437c5' => $updatedISO,
            ];

            // Assign ACF fields with values
            foreach($staff_fields as $key => $name) {
                update_field($key, $name, $inserted_staff);
            }

            // Set featured image from URL via FIFU plugin
            fifu_dev_set_image($inserted_staff, $ImageUrl);

            }

        add_action('wp_ajax_get_staff_from_sharepoint', 'get_staff_from_sharepoint');

    };

Leave a Comment