Skip Navigation

[Resolved] Custom select field pulling dynamic data

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

Last updated by Shane 3 years, 1 month ago.

Assisted by: Shane.

Author
Posts
#2189147
Captura de Pantalla 2021-10-06 a la(s) 15.08.50.png

Hi there,
I'm the same user from this past thread: https://toolset.com/forums/topic/custom-select-field-with-dynamic-data-2/
And again, with exactly the same issue. :/

"I have a post field group from a custom post type (Products) that uses a custom select field ('winery-select'). I would like this custom select field to be dynamic and pull data from my other custom post type (winery)."

The snippet has been created for the case and used to work fine. However, now it's not pulling data anymore.

Here's the snippet's code:

 <?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'        => 'winery',
                '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;
}

Any ideas?

Thanks for your time,
Luciana

#2189175

Shane
Supporter

Languages: English (English )

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

Hi Luciana,

Can you provide me with admin access so that I can have a look at this one in a bit more detail for you.

Thanks,
Shane

#2189223

Shane
Supporter

Languages: English (English )

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

Hi Luciana,

I had a look and it should now be working.

Thanks,
Shane

#2189847
Captura de Pantalla 2021-10-07 a la(s) 10.43.53.png

Hi Shane, thanks for your quick response. Could I know the issue, so I can check it later if it suddenly happens again?

Also, the same field is giving me trouble, not loading all its variables on the view search I created.
Do you think that could be related to the first problem?

Thanks,
Luciana

#2189903

Shane
Supporter

Languages: English (English )

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

Hi Luciana,

The issue was with the field's name in the code. There was a common 'w' when it should've been captailized.

Secondly given that there isn't a predefined list of items in the custom field database it will only show the items that are selected. This means it won't show all the available options only the ones that have already been selected from your posts.

This is one of the drawbacks when dynamically populating the select field.

Thanks,
Shane