# Dynamically Populating Drop Down with custom field

**URL:** <https://community.gravityforms.com/t/dynamically-populating-drop-down-with-custom-field/4606>\
**Category:** Get Help\
**Tags:** populate-anything, dynamic-population, gravity-wiz\
**Created:** [April 27, 2020, 8:22am UTC](https://community.gravityforms.com/t/dynamically-populating-drop-down-with-custom-field/4606 "2020-04-27T08:22:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![webmgt](https://avatars.discourse-cdn.com/v4/letter/w/77aa72/32.png) [@webmgt](https://community.gravityforms.com/u/webmgt)\
**Post date:** [April 27, 2020, 8:22am UTC](https://community.gravityforms.com/t/dynamically-populating-drop-down-with-custom-field/4606/1 "2020-04-27T08:22:30Z")

</div>

Hi, I’m trying to use this [GF docs as an example](https://docs.gravityforms.com/dynamically-populating-drop-down-fields/#creating-an-array-of-choices) 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;
}
```

---

<div class="post-metadata">

**Author:** ![webmgt](https://avatars.discourse-cdn.com/v4/letter/w/77aa72/32.png) [@webmgt](https://community.gravityforms.com/u/webmgt)\
**Post date:** [April 27, 2020, 8:31am UTC](https://community.gravityforms.com/t/dynamically-populating-drop-down-with-custom-field/4606/2 "2020-04-27T08:31:56Z")

</div>

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;
}
```

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [April 27, 2020, 1:58pm UTC](https://community.gravityforms.com/t/dynamically-populating-drop-down-with-custom-field/4606/3 "2020-04-27T13:58:52Z")

</div>

Is that working for you @webmgt?

---

<div class="post-metadata">

**Author:** ![webmgt](https://avatars.discourse-cdn.com/v4/letter/w/77aa72/32.png) [@webmgt](https://community.gravityforms.com/u/webmgt)\
**Post date:** [April 27, 2020, 2:28pm UTC](https://community.gravityforms.com/t/dynamically-populating-drop-down-with-custom-field/4606/4 "2020-04-27T14:28:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [April 27, 2020, 7:45pm UTC](https://community.gravityforms.com/t/dynamically-populating-drop-down-with-custom-field/4606/5 "2020-04-27T19:45:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![david](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/david/32/2517_2.png) [@david](https://community.gravityforms.com/u/david)\
**Post date:** [May 6, 2020, 7:57pm UTC](https://community.gravityforms.com/t/dynamically-populating-drop-down-with-custom-field/4606/6 "2020-05-06T19:57:34Z")

</div>

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.

> **[Gravity Forms Populate Anything - Gravity Wiz](http://gravitywiz.com/documentation/gravity-forms-populate-anything/)**
>
> Dynamically filter and populate field choices and values with posts, users, taxonomies, terms, Gravity Forms entries, and databases. Pretty much anything!

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

 ![Populate Anything - Populate custom post type](https://gwiz.io/2xEMa0Q)

---

<div class="post-metadata">

**Author:** ![richardw8k](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/richardw8k/32/2354_2.png) [@richardw8k](https://community.gravityforms.com/u/richardw8k)\
**Post date:** [January 5, 2022, 8:47am UTC](https://community.gravityforms.com/t/dynamically-populating-drop-down-with-custom-field/4606/7 "2022-01-05T08:47:55Z")

</div>

Closed on January 5, 2022, 8:47am UTC


