Skip to content

Instantly share code, notes, and snippets.

@montrealist
Created February 27, 2013 21:39
Show Gist options
  • Save montrealist/5051989 to your computer and use it in GitHub Desktop.
Save montrealist/5051989 to your computer and use it in GitHub Desktop.
How to programmatically insert and update Advanced Custom Fields (ACF) repeater data
<?php
$acf_group_title = 'custom-fields-for-event';
$acf_field_title = 'event_ticket_info';
$acf_field_key = '';
$data_to_insert = array(array("sub_field_1" => "Foo1", "sub_field_2" => "Bar1"), array("sub_field_1" => "Foo2", "sub_field_2" => "Bar2"));
$acf_args = array(
'post_type' => 'acf',
'posts_per_page' => 1,
'post_status' => 'publish',
'acf_field' => $acf_group_title
);
$acf_query = new wp_query( $acf_args );
if ( $acf_query->have_posts() && isset($acf_query->posts[0]) ) {
$meta_values = get_post_meta($acf_query->posts[0]->ID);
foreach ($meta_values as $key => $value) {
if ( substr($key, 0, 6) == "field_" ) {
$field_params = maybe_unserialize( $value[0] );
if( isset($field_params['name']) && $field_params['name'] == $acf_field_title )
$acf_field_key = $field_params['key'];
}
}
}
if( $acf_field_key != '' ) { // key found, can insert info
error_log('acf_field_key found: ' . $acf_field_key);
update_field( $acf_field_key, $data_to_insert, $event_id );
}
?>
@montrealist
Copy link
Author

Cross-posted in ACF support forum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment