I'm trying to figure out the code for the gform_pre_render that will allow me to pull the data stored in my ACF custom field and populate a drop down list.
I'm not trying to create posts or update the database, just an email contact form. I have a multi-selection field in ACF called "charter_types" that works with a custom post type. I need to add a contact form on the post type, and have the Charter Type dropdown field populated with the multiple values entered on each post.
I've tried various code, but just can't quite get this figured out. Example if this will help.
Post 1: field "charter_type" has 2 values stored, "offshore" and "fly".
Post 2: field "charter_type" has 3 values store, "offshore", "fly" and "tarpon".
When displaying gform 1 on Post 1, the Drop-Down field labeled Select Charter Type needs to display only "offshore" and "fly".
When displaying gform 1 on Post 2, the Drop-Down field needs to display "offshore", "fly" and "tarpon".
This code seems to be the closest, but I'm getting an invalid argument supplied on the for each.
add_filter("gform_pre_render", "populate_dropdown");
//Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
add_filter("gform_admin_pre_render", "populate_dropdown");
function populate_dropdown($form){
//only populating drop down for form id 1
if($form["id"] != 1)
return $form;
//Reading values from custom field charter_types
$chartertypes = get_field('charter_type');
//Creating drop down item array.
$items = array();
//Adding initial blank value.
$items[] = array("text" => "", "value" => "");
//Adding charter type values to the items array
foreach($chartertypes as $chartertype)
$items[] = array("value" => $chartertype, "text" => $chartype);
//Adding items to field id 4. Replace 4 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach($form["fields"] as &$field)
if($field["id"] == 4){
$field["choices"] = $items;
}
return $form;
I think this might be related to another post talking about how ACF stores these values serialized in the DB.
Is there any solution I can use here?