Skip Navigation

[Resolved] Filling a select box from terms in a taxonomy

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

Problem:
Filling a select box from terms in a taxonomy

Solution:
You can use filter "wpt_field_options" in order to fill custom select field options dynamically.

You can find the proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/filling-a-select-box-from-terms-in-a-taxonomy/#post-1123671

Relevant Documentation:

This support ticket is created 6 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.

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 10 replies, has 2 voices.

Last updated by johnnyH-3 6 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1123537

I have a custom post type with a Select field with the title "Nationalitet" (slug: runner_nationality). And I have a taxonomy titled "Nationaliteter" (slug: nationalities). Now I want to fill the select box with the terms in that taxonomy, when adding/editing posts in the admin.

This is the code that I've added to my child-theme's functions.php, taken from https://toolset.com/forums/topic/i-wish-i-could-load-a-custom-select-with-options-from-a-custom-taxonomy/:

//dynamically populate select field for Nationality
add_filter( 'wpt_field_options', 'fill_select', 10, 3);
function fill_select( $options, $title, $type ) {
    if ($title == 'Nationalitet')  {
        $options = array();
        $args = array('hide_empty' => 'false');
        $terms = get_terms( array('nationalities'), $args );
          
        foreach ($terms as $term) {
            $options[] = array(
                '#value' => $term->term_id,
                '#title' => $term->name
            );
        }
    }
}

But the select box is empty... What have I missed?

#1123671

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - It seems you are not returning the $options that is why your select field remains empty. Could you please try to use the following code and try to resolve your issue.

add_filter( 'wpt_field_options', 'func_dynamic_populate', 10, 3);
function func_dynamic_populate( $options, $title, $type ){
    switch( $title ){
        case 'Nationalitet':
            $options = array();
           $options = array();
        $args = array('hide_empty' => 'false');
        $terms = get_terms( array('nationalities'), $args );

            foreach ($terms as $term) {
            $options[] = array(
                '#value' => $term->term_id,
                '#title' => $term->name
            );
           }
            }
            break;
    }
    return $options;
}
#1123677

Thanks. However, that generates a blank page. In the Console I get the following message:
Uncaught TypeError: Cannot read property 'value' of null
at checkIframe (serp.js:140)
at HTMLDocument.<anonymous> (serp.js:9)
at j (jquery.min.js:2)
at k (jquery.min.js:2)

#1123688

Minesh
Supporter

Languages: English (English )

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

Doh - my bad.

There were missing the closing bracket just after the return statement. I've corrected that.

Please use the code:
=> https://toolset.com/forums/topic/filling-a-select-box-from-terms-in-a-taxonomy/#post-1123671

#1123689

Great. One step ahead. The drop-down now shows "Not set". But still no terms... even though nationalities contain one test term so far.

#1123690

Also seems like there are too many closing brackets now...

#1123691

Minesh
Supporter

Languages: English (English )

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

Well - I need to check why its not filling the term.

Could you please share problem URL and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1124017

After some testing with other taxonomies I have realized that if a term is used in a post type, then it will also show up in the Select box. But in this case I will only use terms via the select box field... And I will only add new terms via the page for editing terms for a taxonomy.

How then can we change the code to find all terms, whether they are used in a post type or not?

#1124026

Actually, I found the solution here: https://toolset.com/forums/topic/dynamically-populate-select-field-with-custom-taxonomy/page/2/#post-820284

It seems like the code to get terms was not correct, in that it had to be 'taxonomy' => 'nationalities'.

Now if only I could have the select box not to have the first term selected by default...

#1124329

Minesh
Supporter

Languages: English (English )

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

Well - to add the default option to your select, please add the following line of code just before foreach loop.

	$options[] = array(
        			'#value' => 0,
        			'#title' => "Please Select",
      			);
#1124337

My issue is resolved now. Thank you!