Dropdown populated by Custom post type and associated acf field [RESOLVED]

Apologies for asking this and it may be outside the scope of the group but I have to ask. This is driving me crazy at the moment. I’m waiting for the penny to drop but no such luck.

I will explain what I am trying to do in short.

Use the custom post type and associated ACF field below it.

Have gravity forms populate a dropdown to display all the ACF ra_code entries for each associated custom post type.

At the moment I have tried all manner of combinations from this page with no success.

Does anyone have an idea of how this is achieved? I have the correct form (my case 3) and field number. I’m guessing the missing part is the correlation between custom post type and ACF field. All results just yield a dropdown with – Choose a Department –


I have the following

Custom post type of class_location

Under the custom post type I have associated an acf text field (below is displayed under tools in acf front end)

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array(
	'key' => 'group_5f99bd24b2385',
	'title' => 'RA Code',
	'fields' => array(
		array(
			'key' => 'field_5f99bd42ab911',
			'label' => 'RA Code',
			'name' => 'ra_code',
			'type' => 'text',
			'instructions' => 'Please insert the RA code this is important for the forms.',
			'required' => 1,
			'conditional_logic' => 0,
			'wrapper' => array(
				'width' => '',
				'class' => '',
				'id' => '',
			),
			'default_value' => '',
			'placeholder' => '',
			'prepend' => '',
			'append' => '',
			'maxlength' => '',
		),
	),
	'location' => array(
		array(
			array(
				'param' => 'post_type',
				'operator' => '==',
				'value' => 'class_location',
			),
		),
	),
	'menu_order' => 0,
	'position' => 'normal',
	'style' => 'default',
	'label_placement' => 'top',
	'instruction_placement' => 'label',
	'hide_on_screen' => '',
	'active' => true,
	'description' => '',
));

endif;

Indeed the ACF appears on the custom post type edit page. I require a way of reflecting this in the gravity forms field.

I will reply to myself for posterity. Got it working it has been a long day. The final solution is a variation on the gravity forms page found at

Dynamically Populating Drop Down, Radio Buttons or Multi-Select Fields - Gravity Forms

add_filter( 'gform_pre_render_3', 'populate_posts_custom' );
add_filter( 'gform_pre_validation_3', 'populate_posts_custom' );
add_filter( 'gform_pre_submission_filter_3', 'populate_posts_custom' );
add_filter( 'gform_admin_pre_render_3', 'populate_posts_custom' );
function populate_posts_custom( $form ) {
 
    foreach ( $form['fields'] as $field ) {
 
		//$field->type != 'select' || 
		
        if ( strpos( $field->cssClass, 'populate-posts' ) === false ) {
            continue;
        }
 
        $posts = get_posts( 'numberposts=-1&post_type=class_location&order=ASC' );
 
        $choices = array();
 
        foreach ( $posts as $post ) {
            $choices[] = array( 'text' => $post->ra_code, 'value' => $post->ra_code );
        }
 
        // update 'Select a Post' to whatever you'd like the instructive option to be
        $field->placeholder = 'Select a Post';
        $field->choices = $choices;
 
    }
 
    return $form;
}
1 Like

Thank you for sharing that. I’m certain that will help someone in the future.