Skip Navigation

[Resolved] Populate dropdown with empty default

This support ticket is created 3 years, 11 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Puntorosso 3 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1931923

I populate a select field in a form with the content of a CPT

add_filter( 'wpt_field_options', 'prefix_custom_options', 10, 3);
 function prefix_custom_options( $options, $title, $type ){
    switch( $title ){
        case 'Objekt Name':
            $options = array();
            $args = array(
                'post_type'        => 'objekt',
                'post_status'      => 'publish',
                'posts_per_page'   => -1,
                'orderby' => 'title',
				'order' => 'ASC'
      );
            $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/empty default value in the dropdown?
As it is now I always see the first CPT name

#1932293

Maybe you could insert a blank item in the $options array when you define it:

...
case 'Objekt Name':
  $options = array(
    array(
      '#value' => '',
      '#title' => '',
    )
  );
...

I'd try that first. The wpt_field_options API is undocumented and incompatible with some Views custom search features and some Forms features. Use it at your own risk.

#1938415

It seems to work! Thanks a lot.