Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter marcelo2605

    (@marcelo2605)

    Found a solution using gform_pre_render hook.

    @marcelo2605 would you be kind to share your workaround to this issue?

    Thread Starter marcelo2605

    (@marcelo2605)

    @maelga I don’t remember well what was done. But I think I added the correct option as the first option in the list (see the comment line) and remove it in the loop. Hope this help.

    $populate_comic_dropdown = function( $form ) {
    
    	global $post;
    	$parent_id = $post->post_parent;
    	$parent = get_post( $parent_id );
    	$parent_name = $parent->post_title;
    
    	$args = ['posts_per_page' => -1, 'post_type' => 'comics', 'post_parent' => '0' ];
        $posts = get_posts( $args );
    
        $items = [];
    
        // here I added the correct option as first in the dropdown
        $items[] = [ 'text' => $parent_name, 'value' => $parent_id ];
    
        foreach ( $posts as $post ) {
        	if($post->ID != $parent_id)
            	$items[] = [ 'value' => $post->ID, 'text' => $post->post_title ];
        }
    
        foreach ( $form['fields'] as &$field ) {
            if ( $field->id == 3 ) {
                $field->choices = $items;
            }
        }
    
        return $form;
    };
    
    add_filter( 'gform_pre_render_12', $populate_comic_dropdown );
    add_filter( 'gform_pre_validation_12', $populate_comic_dropdown );
    add_filter( 'gform_pre_submission_filter_12', $populate_comic_dropdown );

    Thanks @marcelo2605. I’ll look into this.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Select field not show selected option’ is closed to new replies.