Skip Navigation

[Resolved] custom SELECT field with dynamic data

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

Problem:

The problem here is that the user wanted to dynamically populate their select field with the information of another post type.

Solution:

This can be done by using the function below.

// Put the code of your snippet below this comment.
 
add_filter( 'wpt_field_options', 'func_dynamic_populate', 10, 3);
      
function func_dynamic_populate( $options, $title, $type ){
    switch( $title ){
        case 'winery-select': //select field slug goes here
  
            $options = array();
            $args = array(
                'post_type'        => 'wineries',
                '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;
}

This can be added to your Toolset Custom code section at Toolset -> Settings -> Custom Code.

Relevant Documentation:
For further information on this code you can have a look at our documentation below.
https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

This support ticket is created 3 years, 2 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by Luciana 3 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#2170223

Tell us what you are trying to do?
I'm creating a post field group from a custom post type (Products) that uses a custom select field. I would like this custom select field to be dynamic and load data from another custom post type (Wineries):

I found my issue to be similar to what was asked and explained in this thread: https://toolset.com/forums/topic/custom-select-field-with-dynamic-data/
So, I'm following the first reply from Minesh, and also the given further info: https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

I've already created a new snippet called 'winery_selection' in the Toolset Custom code snippets and added the code above.

However, I'm in doubt about how to twitch it to adjust my situation.
1) Where the function asks for the 'field-name' that I should personalize, should I write the new select field name I'm creating (Winery-producer) or the old one from which I want info to be loaded (Winery)?

2) Where the function is asking for a 'post_type' that I should personalize to adjust my case, is it the custom post type where the data is created (Wineries) or the other custom post where I want data to be loaded to be selected (Products)?
I hope my question is clear.

What is the link to your site? hidden link

Thanks!

#2170305

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Luciana,

Thank you for getting in touch.

1) Where the function asks for the 'field-name' that I should personalize, should I write the new select field name I'm creating (Winery-producer) or the old one from which I want info to be loaded (Winery)?

That is correct you will put the slug of your custom field.

2) Where the function is asking for a 'post_type' that I should personalize to adjust my case, is it the custom post type where the data is created (Wineries) or the other custom post where I want data to be loaded to be selected (Products)?

You will put the Post Type slug that you want the function to pull the list from. SO if your select field is on the Products Post type then you will use the Wineries post type slug.

Thanks,
Shane

#2171169
Captura de Pantalla 2021-09-16 a la(s) 12.52.43.png

Hi Shane, thanks for your quick reply!

So, I gave the function the "wineries" post_type slug and put the slug of the custom field I'd like data to be pulled from ('winery').

How is the function going to recognize I want it to dynamically populate the options of my select custom field now ('Winery-select')? How do I connect the function with my custom select field?

This is my snippet code:

<?php
/**
 * Dynamic content for 'Winery-select' custom select field in custom Post type. */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

add_filter( 'wpt_field_options', 'func_dynamic_populate', 10, 3);
     
function func_dynamic_populate( $options, $title, $type ){
    switch( $title ){
        case 'winery':
 
            $options = array();
            $args = array(
                'post_type'        => 'wineries',
                '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;
}

Thanks,
Luciana

#2171207

Ok, I re-read everything and realized my coding was wrong since the custom field I had to put in it was the selection one (winery-select), not the feeder one. In that case, how will the function know I need it to pull the info from the slug 'winery'?

Needless to say, the prev screenshot is still valid, since it's still not working. 😀
--

This is my code now:

<?php

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter( 'wpt_field_options', 'func_dynamic_populate', 10, 3);
     
function func_dynamic_populate( $options, $title, $type ){
    switch( $title ){
        case 'winery-select':
 
            $options = array();
            $args = array(
                'post_type'        => 'wineries',
                '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;
}

Thanks again,
Luciana

#2171273

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Luciana,

Would you mind allowing me to have admin access to the website so that I can have a look at this for you ?

I've enabled the private field for your next response.

Thanks,
Shane

#2171285

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Luciana,

This should now be working. What I did was to change the slug to the correct one, you were using "wineries" when it should be "winery"

Thanks,
Shane

#2171291

Thanks a lot for your quick and kind support. My issue is solved now. ***

Luciana