Advertisement
HarunRRayhan

Pull Post Type on Gravity Forms Select

Jun 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. add_filter( 'gform_pre_render', 'populate_choices' );
  2. add_filter( 'gform_pre_validation', 'populate_choices' );
  3. add_filter( 'gform_pre_submission_filter', 'populate_choices' );
  4. function populate_choices( $form ) {
  5.  
  6.     //only populating drop down for form id 3
  7.     if ( $form['id'] != 3 ) {
  8.        return $form;
  9.     }
  10.  
  11.     global $post;
  12.     $post_id = $post->ID;
  13.     $event_start_date = get_post_meta( $post->ID, '_EventStartDate')[0];
  14.     $event_venue_ID = get_post_meta( $post->ID, '_EventVenueID')[0];
  15.  
  16.     foreach ( $form['fields'] as &$field ) {
  17.  
  18.         // Adding Data to HTML Field to test it.
  19.        
  20.         if( $field->id == 13 && $field->type == 'select'  ){
  21.             $choices = array();
  22.             $same_night_events = array();
  23.             $same_night_events[] = $post_id;
  24.             $events_query = new WP_Query(array(
  25.                 'post_type'     => 'tribe_events',
  26.                 'posts_per_page'    => -1,
  27.                 'tax_query' => array(
  28.                     array(
  29.                         'taxonomy' => 'tribe_events_cat',
  30.                         'field'    => 'slug',
  31.                         'terms'    => 'series-classes',
  32.                     ),
  33.                 ),
  34.                 'post__not_in' => array($post->ID),
  35.                 'meta_query' => array(
  36.                     'relation' => 'AND',
  37.                     array(
  38.                         'key'     => '_EventStartDate',
  39.                         'value'   => $event_start_date,
  40.                         'compare' => '=',
  41.                     ),
  42.                     array(
  43.                         'key'     => '_EventVenueID',
  44.                         'value'   => $event_venue_ID,
  45.                         'compare' => '=',
  46.                     ),
  47.                 ),
  48.  
  49.             ));
  50.  
  51.             $all_events = array();
  52.             if($events_query->have_posts()){
  53.                 while ($events_query->have_posts()) {
  54.                     $events_query->the_post();
  55.  
  56.                     $same_night_events[] = get_the_id();
  57.  
  58.                     $all_events[get_the_id()] = get_the_title();
  59.                     $choices[] = array(
  60.                         'text' => get_the_title(),
  61.                         'value' => get_the_title(),
  62.                         'attributes' => array(
  63.                                         'data-event-id' => get_the_id(),
  64.                         ),
  65.                     );
  66.  
  67.                 }
  68.             }
  69.             wp_reset_query();
  70.  
  71.             update_post_meta($post_id, 'same_night_events', $same_night_events);
  72.  
  73.             $field->placeholder = 'Click to select';
  74.             $field->choices = $choices;
  75.         }
  76.     }
  77.  
  78.  
  79.     return $form;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement