In my work on the ACF Custom Database Tables plugin, I write a lot of automated tests to ensure consistent handling as the plugin’s codebase evolves. A big part of this involves updating ACF fields using the update_field() function and then checking to make sure the data is stored in the database as expected and returned as it should when using get_field() to retrieve it.

Whilst most ACF field types have simple data structures, some are more complex and can take some figuring out to determine the correct structure.

Here’s an example of how I use PHP to update an ACF Google Map field programmatically when setting up the tests:

// The field accepts a value with this structure
$value = [
'address' => '123 Example St, Townsville XYZ 1234, Country',
'lat' => – 38.1486228,
'lng' => 144.360414,
'zoom' => 14,
'place_id' => 'Ei0xMjMgTW9vcmFib29sIFN0LCBHZWVsb25nIFZJQyAzMjIwLCBBdXN0cmFsaWEiMBIuChQKEgmX0JaIHBTUahFyH_LC9sYD8hB7KhQKEglDz-DYDxTUahECZY8QisCjzg',
'street_number' => 123,
'street_name' => 'Example Street',
'street_name_short' => 'Example St',
'city' => 'Townsville',
'state' => 'Statename',
'state_short' => 'XYZ',
'post_code' => 1234,
'country' => 'Country',
'country_short' => 'ZX',
// If you need a new post, create one.
$post_id = wp_insert_post(['post_type' => 'post', 'post_status' => 'publish']);
// Update the ACF field for a given post.
update_field($field_name, $value, $post_id);

Keep up to date with all things ACF!

Subscribe to our newsletter or follow @awesomeacf on Twitter!