Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter( 'gform_pre_render', 'populate_choices' );
- add_filter( 'gform_pre_validation', 'populate_choices' );
- add_filter( 'gform_pre_submission_filter', 'populate_choices' );
- function populate_choices( $form ) {
- //only populating drop down for form id 3
- if ( $form['id'] != 3 ) {
- return $form;
- }
- global $post;
- $post_id = $post->ID;
- $event_start_date = get_post_meta( $post->ID, '_EventStartDate')[0];
- $event_venue_ID = get_post_meta( $post->ID, '_EventVenueID')[0];
- foreach ( $form['fields'] as &$field ) {
- // Adding Data to HTML Field to test it.
- if( $field->id == 13 && $field->type == 'select' ){
- $choices = array();
- $same_night_events = array();
- $same_night_events[] = $post_id;
- $events_query = new WP_Query(array(
- 'post_type' => 'tribe_events',
- 'posts_per_page' => -1,
- 'tax_query' => array(
- array(
- 'taxonomy' => 'tribe_events_cat',
- 'field' => 'slug',
- 'terms' => 'series-classes',
- ),
- ),
- 'post__not_in' => array($post->ID),
- 'meta_query' => array(
- 'relation' => 'AND',
- array(
- 'key' => '_EventStartDate',
- 'value' => $event_start_date,
- 'compare' => '=',
- ),
- array(
- 'key' => '_EventVenueID',
- 'value' => $event_venue_ID,
- 'compare' => '=',
- ),
- ),
- ));
- $all_events = array();
- if($events_query->have_posts()){
- while ($events_query->have_posts()) {
- $events_query->the_post();
- $same_night_events[] = get_the_id();
- $all_events[get_the_id()] = get_the_title();
- $choices[] = array(
- 'text' => get_the_title(),
- 'value' => get_the_title(),
- 'attributes' => array(
- 'data-event-id' => get_the_id(),
- ),
- );
- }
- }
- wp_reset_query();
- update_post_meta($post_id, 'same_night_events', $same_night_events);
- $field->placeholder = 'Click to select';
- $field->choices = $choices;
- }
- }
- return $form;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement