Skip to content

Instantly share code, notes, and snippets.

@tmoitie
Last active April 18, 2024 18:56
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tmoitie/9808555 to your computer and use it in GitHub Desktop.
Save tmoitie/9808555 to your computer and use it in GitHub Desktop.
Wordpress: Gravity Forms List to ACF Repeater post save
<?php
add_action('gform_after_submission', 'gfToAcfListToRepeater', 10, 2);
function gfToAcfListToRepeater($entry, $form)
{
foreach ($form['fields'] as $field) {
if (!($field['type'] == 'post_custom_field' && $field['inputType'] == 'list' && $field['enableColumns'] == true)) {
continue;
}
$id = $field['id'];
$postId = $entry['post_id'];
$acfFields = unserialize($entry[$id]);
$fieldName = $field['postCustomFieldName'];
$count = 0;
foreach ($acfFields as $item) {
foreach ($item as $key => $value) {
$acfKey = str_replace(' ', '_', strtolower($key));
$acfFieldName = $fieldName . '_' . $count . '_' . $acfKey;
update_post_meta($postId, $acfFieldName, $value);
}
$count++;
};
update_post_meta($postId, $fieldName, count($acfFields));
}
}
@alexandrac02
Copy link

alexandrac02 commented Apr 18, 2024

Hi! I have the same issue as some of the other users. I've tested all of these code snippets without success. My setup involves a GF list with 2 columns, one is text and the second column is a select. I used @mkormendy snippet as we also use Advanced Custom Post creation to make our listings. I am also using the Post Update Add-On – Gravity Forms for the form to update the rest of the field to ACF. How would I be able to post my double column list to the ACF repeater? Currently nothing happens with the code in the functions.php.

add_action('gform_advancedpostcreation_post_after_creation_2', 'partners_list_field_to_acf_repeater', 10, 4);
function partners_list_field_to_acf_repeater($post_id, $feed, $entry, $form) {

  // set these based on form fields and acf/postmeta fields
  $gf_list_field_id = 71;
  $acf_repeater_field_name = 'service_cities';

  $acf_fields = maybe_unserialize(rgar($entry, $gf_list_field_id));
  $count = 0;
  foreach ($acf_fields as $item) {
    foreach ($item as $key => $value) {
      $acf_key = str_replace(' ', '_', strtolower($key));
      $postmeta_field_name = $acf_repeater_field_name . '_' . $count . '_' . $acf_key;
      update_post_meta($post_id, $postmeta_field_name, $value);
    }
    $count++;
    delete_post_meta($post_id, $acf_repeater_field_name, $value);
  };
  add_post_meta($post_id, $acf_repeater_field_name, count($acf_fields));
}

My two GF list fields are City & State. The ID of the field is 71.
For the ACF repeater the field name is service_cities.
The two fields inside of the ACF repeater is City Name (city_name) and State (state). Would there be a way to make this auto update with the stack?

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