Skip Navigation

[Resolved] Blank default option in pre-populated select field

This support ticket is created 4 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by lewisR 4 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1632459
Screen Shot 2020-05-20 at 5.14.57 PM.png
Screen Shot 2020-05-20 at 5.10.40 PM.png

Hello, I have created a pre-populated field with the code below:

add_filter( 'wpt_field_options', 'populate_select_1_1', 10, 3);
function populate_select_1_1( $options, $title, $type ){
	// Custom List of Speakers
	switch( $title ){
        case 'Presentation Speaker':
            $options = array();
            $args = array(
                'post_type'        => 'mdsc-speakers',
                'posts_per_page'   => -1,
                'post_status'      => 'publish',
            );
            $posts_array = get_posts( $args );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title, 
                     );
                }
                   
			break;
	}
    return $options;
	
}

Is it possible to have a blank default option at the top of the select list so that I can check to see if the field is set? For example, in the attached screenshot, I would like it to start with "Select Speakers" so that I can check to see if the field value is "true" or not.

A side question: is it possible to remove "Enter your internal title for the item" for repeatable groups?

#1632795

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To add a default option using the wpt_field_options hook, you can add it by adding the code:

   $options[] = array(
            '#value' => 0,
            '#title' => "Please Select",
        );

For example:
- Please try to use the following code and check if that helps you to resolve your issue.

add_filter( 'wpt_field_options', 'populate_select_1_1', 10, 3);
function populate_select_1_1( $options, $title, $type ){
    // Custom List of Speakers
    switch( $title ){
        case 'Presentation Speaker':
            $options = array();
            $args = array(
                'post_type'        => 'mdsc-speakers',
                'posts_per_page'   => -1,
                'post_status'      => 'publish',
            );
            $posts_array = get_posts( $args );

           $options[] = array(
            '#value' => 0,
            '#title' => "Please Select",
        );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title, 
                     );
                }
                    
            break;
    }
    return $options;
     
}

I have split the ticket for your additional question:
"Enter your internal title for the item" for repeatable group

I will handle it with the following ticket:
=> https://toolset.com/forums/topic/split-blank-default-option-in-pre-populated-select-field-how-to-change-reove-enter-your-internal-title-for-the-item-for-repeatable-group-field/

#1633877

Thank you!