3

I am trying to populate a dropdown field with an array of my custom post type "color".
with this: https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/

add_filter( 'gform_pre_render_51', 'color_posts' );
add_filter( 'gform_pre_validation_51', 'color_posts' );
add_filter( 'gform_pre_submission_filter_51', 'color_posts' );
add_filter( 'gform_admin_pre_render_51', 'color_posts' );
function color_posts( $form ) {

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

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

    // you can add additional parameters here to alter the posts that are retrieved
    // more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
    $args = array( 'post_type' => 'color' );
    $posts = get_posts( $args );

    $choices = array();

    foreach ( $posts as $post ) {
        $choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
    }

    // update 'Select a Post' to whatever you'd like the instructive option to be
    $field->placeholder = 'Select a Post';
    $field->choices = $choices;

}

return $form;
}

but even after I set a custom css style to "color_posts" then nothing happens. Anything I am doing wrong or could I do this differently.

1
  • This question would be best addressed in the official support channels for Gravity Forms.
    – bosco
    Commented Apr 3, 2017 at 7:46

1 Answer 1

3

The id to your form should be 51 for your code to work, hope the code is placed in functions.php

For gform_pre_render_51, 51 is the id of the form whose select field you are trying to modify

1
  • @MadsI your welcome!
    – IAmDhar
    Commented Apr 5, 2017 at 5:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.