Dynamically Populating Drop Down Fields

Hi folks,
I’m looking for a little bit of help with regards populating a drop down field that is within a custom post type.

I can do this with custom post types & taxonomies however I’m stuck with trying to populate with a sub field (ACF)

Here is what I have.
I have a custom post type called ‘advanced_course’
I have an ACF repeater field ‘pt_subject_courses’
The ACF sub field is ‘pt_subject_course_title’

I’m looking to populate a dropdown field on gravity forms with what ever is in the '‘pt_subject_course_title’ sub field of the repeater field ‘pt_subject_courses’

Could anyone direct me to any existing code snippet or give me a shove in the right direction, I’m not looking for another plugin to do this if possible.

Cheers,
Chris

Matters on how the data is being stored. i.e. if you were going to retrieve just one entry, how would you do it? Usually it is something like…

$args = array(
    'post_type' => 'advanced_course',
    'areas'    => 'pt_subject_courses',
    'order'    => 'ASC'
    );              

$the_query = new WP_Query( $args );
print_r( $the_query );

So if you can use something like that to get the array you want, you can then go from there with a pre_render.

add_filter("gform_pre_render_22_5", "populate_22_5");
add_filter("gform_admin_pre_render_22_5", "populate_22_5");
 function populate_22_5($form){
  if( $form["id"] != 22 )
  
         return $form;
   
  $items = array();
   
//YOUR QUERY START

$args = array(
    'post_type' => 'advanced_course',
    'areas'    => 'pt_subject_courses',
    'order'    => 'ASC'
    );              

$the_query = new WP_Query( $args );

// YOUR QUERY END

   if (is_array($the_query ))
{
     foreach($the_query as $abc) $items[] = array( 'value' => $abc, 'text' => $abc );
}
 foreach($form["fields"] as &$field)
    if($field["id"] == 5){
        $field["choices"] = $items;
    }
return $form;
}

Thank you appreciated. I shall see If I can get anything working out of this.

1 Like