Home › Forums › Add-ons › Repeater Field › Repeater field value not working with Gravity Forms › Reply To: Repeater field value not working with Gravity Forms
Oddly, I posted a followup post last night but it’s completely disappeared…..I still really need help with this please!
I’ve monkeyed around with the code a bit, and now I have it where it displays ALL the repeater field values from the wp_postmeta table, regardless of the Post they are attached to – this is NOT what I want. I want just the values associated with the Post I’m viewing, on which the GF form appears.
I have a Custom Post Type called “Resorts”, and on each Resort Page I have a repeater field to list the room types available for that resort (e.g. Deluxe, Suite, Oceanview, etc.). Each Resort has different room types.
On the (front end) Resort Page, I can display those room types just fine along with the rest of the resort info. ALSO on that Page is a (Gravity) Form for the site visitor to choose a room type and dates and get a price quote. The “room types” is a drop-down field that is supposed to auto-populate with the values from the repeater field.
SO the list of room types to choose from should be isolated to just that Resort – and it’s not, my drop down list is very long and includes ALL of the room types from ALL resorts…..aaargh!
I am NOT a coder, just trying to piece this together using examples from this website that don’t really fit – the examples are mostly for showing ALL field values, such as the “Diplay all images” example.
SO here is the code I’m using now – can someone PLEASE tell me how to modify this to ONLY show the repeater field values in the form dropdown from the one POST I’m viewing?
add_filter("gform_pre_render_3", "dynamic_populate");
function dynamic_populate($form) {
foreach($form['fields'] as &$field){
if(strpos($field['cssClass'], 'dynamicfield') === false)
continue;
//Condition : each field which has the class dynamicfield (example). 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 $wpdb;
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
WHERE meta_key LIKE %s
",
'room_types_%_room_type'
));
$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.
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( $row->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.