-1

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.

closed as off-topic by Mark Kaplun, Dave Romsey, bosco, cjbj, Howdy_McGee Apr 3 '17 at 15:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • This question would be best addressed in the official support channels for Gravity Forms. – bosco Apr 3 '17 at 7:46
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

  • @MadsI your welcome! – IAmDhar Apr 5 '17 at 5:03

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