Skip Navigation

[Resolved] How to dynamically populate a custom field dropdown with terms from a taxonomy

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

Problem:
The client has some custom code that uses the wpt_field_options filter to dynamically populate the options of a select custom field, in this case using the terms of a taxonomy to populate the options, but it's not working.

Solution:
There was a minor error in the code, the correct version of which is shown below: https://toolset.com/forums/topic/dynamically-populate-the-a-dropdown-list-with-taxonomy-terms/#post-658423

This support ticket is created 6 years, 9 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by khalidK 6 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#657287

I am trying to:
Dynamically populate the a Dropdown list of Custom User Field with Custom Taxonomy terms

 
//dynamically populate select field with Taxonomy Terms

add_filter( 'wpt_field_options', 'populate_select', 10, 3);
function populate_select( $options, $title, $type ){
switch( $title ){
    case 'School Name':
        $options = array();
        $args = array(
        'hide_empty' => 'false');
        $terms = get_terms( array('school'),$args );
        foreach ($terms as $term) {
        $options[] = array(
        '#value' => $term->term_id,
        '#title' => $term->name,
        );
    }
    break;
    }
    return $options;
}

For some reason the list comes back frozen without any items

#658015

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Khalid

That looks okay, but I would check the syntax for get_terms, which expects the arguments to be a single array of options: https://developer.wordpress.org/reference/functions/get_terms/

I would then add error_log(print_r($terms, true)); immediately after get_terms and inspect your logs to double-check that you have retrieved the terms correctly.

Let me know what you find.

#658423

I figured out the issue. I only had to replace 'false' with a 0, and it worked.

The final code is below for future reference:

//dynamically populate select field with Taxonomy Terms

add_filter( 'wpt_field_options', 'populate_select', 10, 3);
function populate_select( $options, $title, $type ){
switch( $title ){
    case 'School Name':
        $options = array();
        $args = array(
        'hide_empty' => 0);
        $terms = get_terms( array('school'),$args );
        foreach ($terms as $term) {
        $options[] = array(
        '#value' => $term->term_id,
        '#title' => $term->name,
        );
    }
    break;
    }
    return $options;
}