Support

Account

Home Forums General Issues Passing ACF into Gravity Forms Select Field

Solving

Passing ACF into Gravity Forms Select Field

  • Hi,

    I’ve been able to create a function that passes information from ACF to a Gravity Forms drop down menu. It will successfully load up dates as “selections” in the dropdown. I am not trying to add an ACF field called “location” to the field, but it will not return the value.

    Any ideas on how to get this to work?

    Code is below.

    
    add_filter('gform_pre_render_3', 'populate_dates');
    
    function populate_dates($form){
        
        foreach($form['fields'] as &$field){
            
            if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-dates') === 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
            $currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
            
            $events = get_posts(array(
    				    'post_type' => 'seminars',
    				    'orderby' => 'date',
    				    'order' => 'ASC',
    				    'meta_query'=> array(
    				        array(
    				          'key' => 'date',
    				          'compare' => '>=',
    				          'value' => $currentdate,
    				          'type' => 'DATE',
    				        )),
    				    'meta_key' => 'date',
    				    ));	 
            
            // update 'Select a Post' to whatever you'd like the instructive option to be
            $choices = array(array('text' => 'Requested Seminar Date', 'value' => ' '));
            
            foreach($events as $post){
            
            	$location = get_field("location");
    			$postdate = $post->date;
    			// $postdate = 19881123 (23/11/1988)
    		
    			// extract Y,M,D
    			$y = substr($postdate, 0, 4);
    			$m = substr($postdate, 4, 2);
    			$d = substr($postdate, 6, 2);
    		
    			// create UNIX
    			$time = strtotime("{$d}-{$m}-{$y}");
    		
    			// format date (November 11th 1988)
    			$dropdowndate = date('M d, Y', $time);
    						
    			
    			// not sure if you want to change just the displayed date, or the submitted date as well
    			// if you want to change both, change the second $post->date to $dropdowndate
    		    $choices[] = array('text' => $dropdowndate . $location, 'value' => $post->$dropdowndate);
    		}
    
            
            $field['choices'] = $choices;
            
        }
        
        return $form;
    }
    
  • Additionally, if I assign $location above with $location = "Hey!" it works fine. It does not work if I use the get_field function.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Passing ACF into Gravity Forms Select Field’ is closed to new replies.