N00b PHP question - gform_column_input filter [RESOLVED]

Please can someone provide a suggestion on how to populate a list field column with all the post id’s and post titles from a single custom post type (‘publisher’).

When transforming a list column into a select field, the gform_column_input filter requires each choice to be an array. The format should be:

array( “value” => “1”, “text” => “HarperCollins” ),
array( “value” => “2”, “text” => “Penguin Random House” ),
array( “value” => “3”, “text” => “Macmillan” )

The function I am using to create a key/value array to represent each publisher is:

function choice_option_arrays() {
    $output = "";
    $query = new WP_Query( array( 'post_type' => 'publisher' ) );
    if ( $query->have_posts() ) {
            while ( $query->have_posts() ) : $query->the_post();
                    $output .= 'array( "value" => "' . get_the_id() . '", "text" => "' . get_the_title() . '" ),';
            endwhile;
            wp_reset_postdata();
    }
    return rtrim( $output, ',' );
}

I am then using the result of this function in my gform_column_input filter as shown:

function create_select_field_column( $input_info, $field, $column, $value, $form_id ) {
    return array(
            'type' => 'select',
            'choices' => array(
                    choice_option_arrays()
            )
    );
}
add_filter( 'gform_column_input_1_1_2', 'create_select_field_column', 10, 5 );

How do I get my gform_column_input filter to recognize the output of my choice_option_arrays() function as a series of arrays, rather than as a literal string?

Any suggestions very much appreciated.

Thank you.

After further googling I’ve solved this issue. New choice_option_arrays() function:

function choice_option_arrays() {

    $output = array();

    $query = new WP_Query( array( 'post_type' => 'publisher' ) );

    if ( $query->have_posts() ) {

        while ( $query->have_posts() ) : $query->the_post();

            $output[] = array( "value" => get_the_id(), "text" => get_the_title() );

        endwhile;

        wp_reset_postdata();

    }

    return $output;

}

New gform_column_input filter:

function create_select_field_column( $input_info, $field, $column, $value, $form_id ) {

    return array(
        'type' => 'select',
        'choices' => choice_option_arrays()
    );

}
add_filter( 'gform_column_input_1_1_2', 'create_select_field_column', 10, 5 );

Hope this helps another PHP newbie sometime in the future.

1 Like

Thank you for posting your solution!