Title: Dynamically populate fields
Last modified: February 28, 2018

---

# Dynamically populate fields

 *  Resolved [puzzlepieces](https://wordpress.org/support/users/puzzlepieces/)
 * (@puzzlepieces)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/dynamically-populate-fields-2/)
 * We are using gravity forms and want to dynamically populate the drop-down field
   on the form. It’s currently working but it displays ALL events and not just the
   upcoming events. We updated the themes function.php code but can’t get it to 
   only display upcoming events.
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fdynamically-populate-fields-2%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Spiffy Plugins](https://wordpress.org/support/users/spiffyplugins/)
 * (@spiffyplugins)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/dynamically-populate-fields-2/#post-10028134)
 * How are you currently getting the event list? If you are doing a DB query, you
   could add the condition that event_begin is in the future. Another option is 
   to use the “upcoming_events” function which produces an HTML UL list of the upcoming
   events, then apply some string manipulation to convert it to a SELECT list.
 *  Thread Starter [puzzlepieces](https://wordpress.org/support/users/puzzlepieces/)
 * (@puzzlepieces)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/dynamically-populate-fields-2/#post-10028613)
 * The event list is being dynamically populated from all of our events. We added
   the following code to the themes functions.php file. But we can’t figure out 
   how to get it to just pull the upcoming events and not all events. Can you help
   us with that?
 * add_filter( ‘gform_pre_render_4’, ‘populate_posts’ );
    add_filter( ‘gform_pre_validation_4’,‘
   populate_posts’ ); add_filter( ‘gform_pre_submission_filter_4’, ‘populate_posts’);
   add_filter( ‘gform_admin_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 retrieved
   //
   more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
   $posts = get_posts( ‘post_type=event&numberposts=-1&post_status=published’ );
 *  $choices = array();
 *  foreach ( $posts as $post ) {
    $choices[] = array( ‘text’ => $post->post_title,‘
   value’ => $post->post_title ); }
 *  // update ‘Select a Post’ to whatever you’d like the instructive option to be
   
   $field->placeholder = ‘Select an Event’; $field->choices = $choices;
 *  }
 *  return $form;
    }
 *  Plugin Author [Spiffy Plugins](https://wordpress.org/support/users/spiffyplugins/)
 * (@spiffyplugins)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/dynamically-populate-fields-2/#post-10028700)
 * Looks like you are querying for events from The Events Calendar, not from my 
   Spiffy Calendar plugin.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Dynamically populate fields’ is closed to new replies.

 * ![](https://ps.w.org/spiffy-calendar/assets/icon-256x256.png?rev=2398731)
 * [Spiffy Calendar](https://wordpress.org/plugins/spiffy-calendar/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/spiffy-calendar/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/spiffy-calendar/)
 * [Active Topics](https://wordpress.org/support/plugin/spiffy-calendar/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/spiffy-calendar/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/spiffy-calendar/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Spiffy Plugins](https://wordpress.org/support/users/spiffyplugins/)
 * Last activity: [8 years, 2 months ago](https://wordpress.org/support/topic/dynamically-populate-fields-2/#post-10028700)
 * Status: resolved