Home › Forums › Add-ons › Repeater Field › Repeater field value not working with Gravity Forms › Reply To: Repeater field value not working with Gravity Forms
OK I finally solved this……..spent hours reading and trying things and finally figured out that I had to get the Post ID first, and make another change from the example I first found, so here is the code that WORKS, I’m posting it in case it might help anyone else trying to use a Repeater Field with values stored in wp_postmeta (not options as the example I found is) to populate a GravityForm dropdown, and needs to limit the values to the individual Post.
I added comments where needed, you can remove those if you use this.
// Replaces GravityForms dropdown with list of values from ACF Repeater field
add_filter("gform_pre_render_3", "dynamic_populate");
// replace '_3' with the actual ID from your Gravity Form, mine is form ID 3
function dynamic_populate($form) {
foreach($form['fields'] as &$field){
if(strpos($field['cssClass'], 'dynamicfield') === false)
continue;
//This only auto-populates a field which has the class dynamicfield. If it doesn't then, continue ! Don't forget to add a class to the field you want to dynamically populate in your form.
global $post;
$id = $post->ID; // Important - this is what tells the form to only use values from the individual post, not all posts
global $wpdb;
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
WHERE post_id = %d AND meta_key LIKE %s
",
$id,
'room_types_%_room_type' //enter the actual name of your repeater field and sub-field names, the % is a variable that represents the row number
));
$choices = array(array('text' => 'Select Room Type', 'value' => ' '));
//Default choice for dynamic select field. Value is blank, so if it's required it won't validate, change the text to what you want it to be, e.g. 'Please Choose:'
if( $rows ){
foreach( $rows as $row ) {
//If there are results, for each result, find the 'repeater row number'
preg_match('_([0-9]+)_', $row->meta_key, $matches);
$meta_key = 'room_types_' . $matches[0] . '_room_type';
$value = get_post_meta($post->ID,$meta_key,true);
//Get the subfield value from the row.
$choices[] = array('text' => $value, 'value' => $value);
//Create an option array for each subfield value.
}
}
$field->choices = $choices;
//Once we've looped through all of the subfields values and created all of our options, we assign the $choices array (which has all of our new options) to the $field choices property.
}
return $form;
//Display the new dynamically populated form.
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.