Skip Navigation

[Resolved] Create select custom field with over 100 items

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Waqar 2 months, 2 weeks ago.

Assisted by: Waqar.

Author
Posts
#2695581

Dear Sir/Madam,

How can i build the list for the select custom field with over 100 items, I don't think I should need to add option one by one. Please advise how I can do.

#2695675

Hi,

The Toolset Types offers a filter 'wpt_field_options', which can be used to dynamically populate the options for the 'select' and 'radio' type fields:
https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

You can add that list of over 100 items, as posts in a custom post type or as terms in a custom taxonomy and then use a custom function attached to this filter to generate the options for the target field.

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2695781

Dear Waqar,

If I create custom post to store the items, should this custom function attach to this filter will be dynamically updated when view in from or only import items to field permanently?

I read the usage example but no idea how it related to custom post, do you have more example how this filter linked with custom post.

#2695804

Yes, you'll find an example code snippet for a similar requirement, where a custom post type is used with this filter, in this forum reply:
https://toolset.com/forums/topic/programmatically-add-options-to-select-custom-field-with-a-filter/#post-2320285

> If I create custom post to store the items, should this custom function attach to this filter will be dynamically updated when view in from or only import items to field permanently?

- Because the filter generates the options dynamically, on-the-fly, you won't have to run any periodic imports for these options. And these options for the fields will work while the editing/adding the post from the admin area, as well as through front-end Toolset post forms.

#2695866

Dear Waqar,

I review below code snippet

add_filter( 'wpt_field_options', 'fill_our_people', 10, 3);
function fill_our_people( $options, $title, $type ){
    switch( $title ){
        case 'Team Member Associated':
            $args = array(
                'post_type'        => 'team-member',
                '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;
}

Does it not accept two custom field with same field title but both fields are from different custom post, for example the country list, I may need it for customer to select the country they come from when filling they personal information and the country where they will go when filling the tour they join.

I won't name the field title as From Country and To Country, please advise how the custom field be filtered.

#2695873

Suppose you have two select fields with titles 'Field 1 Title' and 'Field 2 Title' and both need to have options from the post type with slug, 'country'.

The code in this case will look like this:


add_filter( 'wpt_field_options', 'fill_custom_field_options', 10, 3);
function fill_custom_field_options( $options, $title, $type ){
    switch( $title ){
        case 'Field 1 Title':
        case 'Field 2 Title':
            $args = array(
                'post_type'        => 'country',
                '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;
}

And for the case, where your custom fields, need to have options from two different post types, you can use two separate functions:


add_filter( 'wpt_field_options', 'fill_custom_field_1_options', 10, 3);
function fill_custom_field_1_options( $options, $title, $type ){
    switch( $title ){
        case 'Field 1 Title':
            $args = array(
                'post_type'        => 'country',
                '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;
}

add_filter( 'wpt_field_options', 'fill_custom_field_2_options', 10, 3);
function fill_custom_field_2_options( $options, $title, $type ){
    switch( $title ){
        case 'Field 2 Title':
            $args = array(
                'post_type'        => 'city',
                '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;
}

In these two functions, the first one will set options from the 'country' post type for the field with the title 'Field 1 Title'. And the second one will set options from the 'city' post type, for the field with the title 'Field 2 Title'.

I hope these examples will make the usage more clear and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.