Dynamically Populating Drop Down with custom field

Hi, I’m trying to use this GF docs as an example to populate a drop down of my form to show everything under my custom post type and a custom field & selected value.

However I’ve tried it I seem to get nothing coming through, can anyone help with some suggestions or advice please

add_filter( 'gform_pre_render_8', 'populate_free_courses' );
add_filter( 'gform_pre_validation_8', 'populate_free_courses' );
add_filter( 'gform_pre_submission_filter_8', 'populate_free_courses' );
add_filter( 'gform_admin_pre_render_8', 'populate_free_courses' );

function populate_free_courses( $form ) {

    foreach ( $form['fields'] as &$field ) {

        if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-free-courses' ) === false ) {
            continue;
        }

    
        $posts = array( 
            
            'post_type' => 'free_courses',
            'post_status'    => 'publish',
	        'posts_per_page' => -1,
	        
            'meta_query' => array(
		        array(
        			'key' => 'web_enrolments_system',
        			'value' => 'yes',
        		)
	        )
            
            );

        $choices = array();

        foreach ( $posts as $post ) {
            $choices[] = array( 
            	'order'  		=> 'ASC',
    		'orderby'   	=> 'title', 
            	'text' => $post->post_title, 
            	'value' => get_permalink($post->ID)
            	);
        }
    
        $field->placeholder = 'Select a course';
        $field->choices = $choices;

    }

    return $form;
}

I may of solved this always the same… here is what I came up with

add_filter( 'gform_pre_render_8', 'populate_free_courses' );
add_filter( 'gform_pre_validation_8', 'populate_free_courses' );
add_filter( 'gform_pre_submission_filter_8', 'populate_free_courses' );
add_filter( 'gform_admin_pre_render_8', 'populate_free_courses' );

function populate_free_courses( $form ) {

    foreach ( $form['fields'] as &$field ) {

        if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-free-courses' ) === false ) {
            continue;
        }
        
        
        $freeposts = array( 
            
            'post_type' => 'free_courses',
            'post_status'    => 'publish',
	        'posts_per_page' => -1,
	        
            'meta_query' => array(
		        array(
        			'key' => 'web_enrolments_system',
        			'value' => 'yes',
        		)
	        )
            
            );

        $posts = get_posts( $freeposts );
        

        $choices = array();

        foreach ( $posts as $post ) {
            $choices[] = array( 
            	'order'  		=> 'ASC',
    			'orderby'   	=> 'title', 
            	'text' => $post->post_title, 
            	'value' => get_permalink($post->ID)
            	);
        }

    
        $field->placeholder = 'Select a course';
        $field->choices = $choices;

    }

    return $form;
}

Is that working for you @webmgt?

@chrishajer seems to be, unless you have any other suggestions on how I can improve it

So long as it works, the approach looks OK to me.

Great solution, @webmgt!

If you ever need to do this again and don’t want to touch code, Populate Anything can handle this like it ain’t even a thing.

Here’s an example of what your custom code would look like if configured in Populate Anything: