Skip Navigation

[Resolved] Create custom field dropdown populated with values of custom types

This thread is resolved. Here is a description of the problem and solution.

Problem:
Create custom field dropdown populated with values of custom types

Solution:
you should use Types hook 'wpt_field_options' to fill out the options for your user custom dropdown select field.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/create-custom-field-dropdown-populated-with-values-of-custom-types/#post-900936

Relevant Documentation:

This support ticket is created 6 years, 8 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 3 replies, has 2 voices.

Last updated by Minesh 6 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#900925

Hello
I created a new post type called ASSOCIATIONS.
I also created a post type called SOCIETY. When I insert a new post I'd like to see my custom field as a dropdown containing the title of the post s of the type ASSOCIATIONS.
Essentially I'd like to create a custom field which is dinamically populated with the name of the posts of a custom type.

How can I do such a thing??
Thank you
Cristiano

#900936

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - you should use Types hook wpt_field_options to fill out the options for your user custom field.

Lets say, you have created custom field with title "associations select"

For example - please try to add following code to your current theme's functions.php file.:

//dynamically populate select field from Types
add_filter( 'wpt_field_options', 'prefix_custom_options', 10, 3);
 function prefix_custom_options( $options, $title, $type ){
    switch( $title ){
        case 'associations select':
            $options = array();
            $args = array(
                'post_type'        => 'association',
                '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;
}

Where:
- Adjust your post_type slug with above code with your user custom field title. if required.

#901081

Thanks I understood, and all working fine!

#901095

Minesh
Supporter

Languages: English (English )

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

Well - would you mind to resolve the ticket with happy face 🙂 - Have a nice weekend.