On a government site I was recently working on, a feature was requested to their sites employment application, that users ONLY be able to select positions that are currently being advertised. For this, all current job postings go to an Employment Opportunities category.
Gravity Forms is used for the employment application on this site. Therefore, there are two ways you might want to populate a drop down field. The first way is pre-selecting a drop down option when the form is displayed. The second way is dynamically populating the options that are available in a select drop down field.
Adding the following code to the sites functions.php file enabled this ability.
// update the '4' to the ID of your form add_filter('gform_pre_render_4', 'populate_posts'); function populate_posts($form){ foreach($form['fields'] as &$field){ if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-posts') === false) continue; // you can add additional parameters here to alter the posts that are retreieved // more info: http://codex.wordpress.org/Template_Tags/get_posts $posts = get_posts('category=37&post_status=publish'); // update 'Select a Post' to whatever you'd like the instructive option to be $choices = array(array('text' => 'Select the Position Applied For', 'value' => ' ')); foreach($posts as $post){ $choices[] = array('text' => $post->post_title, 'value' => $post->post_title); } $field['choices'] = $choices; } return $form; }
Once the above code was added to the functions.php file. Update the Form ID number 4 to match your form ID.
add_filter('gform_pre_render_4', 'populate_posts');
Then in your Gravity Form, under the Advanced Tab add the CSS class name ‘populate-posts’.
if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-posts') === false)
Grab your category ID and replace where this is ’37’
$posts = get_posts('category=37&post_status=publish');
Change the instructive option to read whatever you want. Notice the value is empty indicating the user must choose a posted option.
$choices = array(array('text' => 'Select the Position Applied For', 'value' => ' '));