Skip Navigation

[Resolved] Dynamically Populate a Select Field with Custom Taxonomy Values

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

Problem:

The issue here is that the user wanted to dynamically populate their select field with the taxonomy values.

Solution:

This can be done by using the code below.

// Prepopulate Select Field with Custom Taxonomy Values
add_filter( 'wpt_field_options', 'add_some_options', 10, 3);
function add_some_options( $options, $title, $type )
{
    switch( $title )
    {
// Select Field Name Not the Slug
    case 'Media Type':
           $terms = get_terms( array(
// Custom Post Type Taxonomy Slug Not the Name
'taxonomy' => 'media-type',
      'hide_empty' => false,
    ) );
        foreach( $terms as $term ) {
            $options[] = array(
                  '#value' => $term->term_id,
        '#title' => $term->name,
            );
        }
        break;
    }
    return $options;
}
This support ticket is created 5 years, 5 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
- 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)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by andrewF-6 5 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#1260533

1. I have the following code that I am hoping to use to automatically populate my select field of the name "Media Type Auto" with a list of items from my Custom Taxonomy Slug "media-type".

I am getting the following error:
"syntax error, unexpected '$options' (T_VARIABLE) in /home/glancelo/public_html/arrirental/wp-content/toolset-customizations/media-type-auto.php on line 16

A problem occurred when executing snippet "media-type-auto". The result of include_once is: ""

Lines 1 to 33 of the Code Snippet entered into the "Toolset Custom code" snippets modules are listed below:

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

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

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

// Prepopulate Select Field with Custom Taxonomy Values
add_filter( 'wpt_field_options', 'fill_select', 10, 3);
function fill_select( $options, $title, $type ){
switch( $title ){
// Select Field Name not the Slug
case 'Media Type Auto':
    $options = array();
    $terms = get_terms( array(
// Custom Post Type Taxonomy Slug not the Name
'taxonomy' => 'media-type',
      'hide_empty' => false,
    ) );
 
    foreach ($terms as $term) {
      $options[] = array(
        '#value' => $term->term_id,
        '#title' => $term->name,
      );
    }
       
    break;
    }
    return $options;
}

Tell us what you are trying to do?
I am trying to have a select field in a custom post type be dynamically populated by the values of a custom taxonomy.

Is there any documentation that you are following?
https://toolset.com/forums/topic/dynamically-populate-select-field-with-custom-taxonomy/page/2/

What is the link to your site?
hidden link

#1260555

Shane
Supporter

Languages: English (English )

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

Hi Andrew,

Thank you for getting in touch.

Not sure why you would want to populate the select tag with the taxonomy name and ID when you can select the taxonomy from the edit post screen.

Also you should be aware that you won't be able to filter your view by these values either.

Please let me know if this is ok.

Thanks,
Shane

#1260565

Hi Shane,

You are actually familiar with this project because you really helped me out with it a day ago! We really appreciate that help too!

Currently in our custom post type we have a select field to group Credit Details together. We have wordpress Taxonomies and are having to manually reenter those taxonomies into the Select Field. But if we could automate that process, every addition or change to the Media Type taxonomy would automatically be reflected in the Media Type Auto Select Field as opposed to the users having to manually synchronize the Media Type Auto Select field with the Media Type Taxonomy.

If this is done, why would we not be able to continue to use this field to filter or layout / group our views?

Thanks

#1260589

Shane
Supporter

Languages: English (English )

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

Hi Andrew,

If it a frontend filter, lets say a Parametric search then the values wouldn't be picked up by views. This is because views will be pulling the select field options from Types.

However if its a static filter you are adding then it should work.

I fixed the code and noticed that you are using this in a RFG . From what I can see now it is slowing down the loading of the RFG or causing it to not load at all.

There is a test field that I created to ensure the code works and it does but because the fields are in a RFG it is causing issues.

I would recommend doing a manual population of the fields to avoid this.

Thanks,
Shane

#1260591

Shane
Supporter

Languages: English (English )

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

Hi Andrew,

Nevermind it seems to be working fine now.

#1260593

Hi Shane,

1. It will only be a static filter which I will use within a custom post type from the admin side;
2. Thank-you for fixing the code, I will study it and figure out how you did it - thanks!
3. If the processing cost is too steep we will continue to manually populate the field - the taxonomy CPT won't be excessively large or exceedingly dynamic.

Thank you so much for your efforts - I will message back here if there are additional questions!

#1260599

Shane
Supporter

Languages: English (English )

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

Hi Andrew,

Great happy i'm able to assist you once again.

You may mark the ticket as resolved once you've completed your checks.

Thanks,
Shane

#1261317

My issue is resolved now. Thank you!

Thanks to Shane my code was modified to work perfectly. You can now easily populate a select field with a Custom Post Type Taxonomy.

// Prepopulate Select Field with Custom Taxonomy Values
add_filter( 'wpt_field_options', 'add_some_options', 10, 3);
function add_some_options( $options, $title, $type )
{
    switch( $title )
    {
// Select Field Name Not the Slug
    case 'Media Type':
           $terms = get_terms( array(
// Custom Post Type Taxonomy Slug Not the Name
'taxonomy' => 'media-type',
      'hide_empty' => false,
    ) );
        foreach( $terms as $term ) {
            $options[] = array(
                  '#value' => $term->term_id,
        '#title' => $term->name,
            );
        }
        break;
    }
    return $options;
}

Thank-you Shane.