Skip Navigation

[Resolved] Display the result from a conditional dropdown selection, on the front end

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

Problem: I would like to create a custom taxonomy term select system in a Form. The parent terms should be listed in one custom select field, and each group of child terms should be listed in a separate select field.

Solution:
Use generic fields to produce the conditional select fields of parent and child terms. Then use the Forms API to capture the selected parent and child terms and store them using wp_set_object_terms.

On the front-end, you can use a View of terms filtered by term ID, supplied by a shortcode attribute, to display information about the selected child term.

Relevant Documentation:
https://toolset.com/forums/topic/parent-child-term-taxonomies-on-cred-and-views/
https://toolset.com/forums/topic/display-taxonomy-associated-with-port-parent/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_set_object_terms

This support ticket is created 5 years, 4 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1166545

Tell us what you are trying to do? The client was not happy with the multi-select. I consulted a developer and we were able to create a category dropdown with 19 categories, and 19 sub-category dropdowns with a conditional to display based on the category selection, using Toolset. Now I need to display the sub-category dropdown result on the member within the member profile page, and on the member search pages via a shortcode.

Is there any documentation that you are following? This was my original support ticket: https://toolset.com/forums/topic/creating-business-category-taxonomy-with-a-sub-category-display-on-front/

Is there a similar example that we can see? hidden link I created Business Category (WANT) and it works as it should, however I need a way to display the result on the front end. Here's the member profile page: hidden link and one of the directory pages: hidden link Currently both category and sub-category display. She wants to only see the sub-category from the new dropdowns created.

What is the link to your site? hidden link

#1167252

Hi, if I understand you correctly, you would like to display only the child term, not the parent term. The wpv-post-taxonomy shortcode is not filterable, so you must use a View of the Business Category taxonomy, filtered by term.
- Add the following custom code snippet in your child theme's functions.php file, or in Toolset > Settings > Custom code:

// DEEPEST HIERARCHICAL TERM IN A TAXONOMY FOR A GIVEN POST
// Shortcode that accepts a post ID and a taxonomy slug and
// returns the slug of deepest hierarchical term assigned
// e.g. [toolset_deepest_term id="1234" tax="business-category"]
add_shortcode( 'toolset_deepest_term', 'toolset_deepest_term_func');
function toolset_deepest_term_func($atts)
{
  $id = $atts['id'];
  $tax = $atts['tax'];
  $terms = wp_get_post_terms( $id, $tax, array( 'orderby' => 'id', 'order' => 'DESC' ) );

  $deepestTerm = false;
  $maxDepth = -1;
  foreach ($terms as $term) {
      $ancestors = get_ancestors( $term->term_id, $tax );
      $termDepth = count($ancestors);
      if ($termDepth > $maxDepth) {
          $deepestTerm = $term;
          $maxDepth = $termDepth;
      }
  }
  return isset($deepestTerm->term_id) ? $deepestTerm->term_id : '-1';
}

- Go to Toolset > Settings > Frontend content, and add toolset_deepest_term in the Third party shortcode arguments section.
- Create a View of the Business Category taxonomy and add a taxonomy term filter, set by a shortcode attribute "terms".
- In the Loop editor, insert the taxonomy link. This will link to the term archive page.
- Insert this View in the template for single Member profiles and add the toolset_deepest_term shortcode as the terms attribute. Like this:

[wpv-view name="Your Term View Name" terms="[toolset_deepest_term id='[wpv-post-id]' taxonomy='business-category']"]

- Confirm this is working as expected, and you can reuse this View in the Search Results template.

#1167264
Screen Shot 2018-12-16 at 6.04.03 PM.png
Screen Shot 2018-12-16 at 6.18.58 PM.png
Screen Shot 2018-12-16 at 6.19.11 PM.png
Screen Shot 2018-12-16 at 6.19.23 PM.png

Can you verify the code: There is only one line and the left to right scroll covers it so I can't read it clearly.

[wpv-view name="sub-category-view" terms="[toolset_deepest_term id='[wpv-post-id]' taxonomy='business-category']"]

I don't think you fully understood. This would work perfectly, If I was using the taxonomy "Business Categories" for both the business category and the sub-category. I had originally set it up this way, however my client felt the multi-select was too difficult for her clients to select the options. So we created a "regular dropdown", in custom fields for both the category and there will be a dropdown for each of the 19 sub-categories (see attached).

I have not removed the taxonomies category/sub yet, which is why I do see how this could work. Only the subcategories show up.

Would it help to have access?

#1167288

I don't think this is going to work, the way I did it. I'm going to need to use the taxonomy and display a main category in 1 dropdown, and a sub-category for the selected in another dropdown. The logic of what I did works, but how do I apply that using the taxonomy?

#1167873

No, I was under the impression your "categories" and "subcategories" were...well, categories and subcategories. Let's back up a second because I'm still not clear, which do you want to use to store the selection - custom fields, or a custom taxonomy? I understand on the front-end you want a specific experience with separate input fields. However, on the backend it's not clear to me because you use the terms "categories" and "subcategories" like WordPress taxonomy terms. I'll list a few pros and cons of each so you can make an informed decision.
- With custom fields, you do not have the benefit of the built-in WordPress archive system. Creating archives for each "category" and "sub-category" term will be a manual process. Hierarchy of "terms" is completely custom, and not easily traversible with Views filters.
- With a custom taxonomy, you have the built-in WordPress archive system that will automatically create archives for each term. Hierarchy is built-in, and Views have term parent filters built in. However to capture the selections in Forms and convert those to terms assigned to the current post will require custom code.

To be clear, either approach requires some customization in Forms to do exactly what you want on the front-end, with separate inputs for top-level and sub-level "terms". The question is how do you want to store this information on the backend of the site.

#1167912

Hi Christian,

I would LOVE to store the category and sub-category taxonomies. I started here, and they are all still in place. The problem arose when the member needs to complete their profile on the front end form and make their selection. The multi-select option was too difficult for the users. I need a Dropdown for Category, and a Dropdown for Sub-Category, that is based on their selection. So if they chose animal care, then only the animal care options show up in the sub-category dropdown.

I couldn't wrap my head around it and after consulting with a developer and trying to utilize Toolset, I was able to simulate what I wanted to happen with the "regular" dropdowns.

I understand some customization is needed, is this something you can walk me through. I've come pretty far in being able to grasp this, it's just outside my skillset.

Thank you.

#1168866

The first step is to create the required generic fields: one for top-level terms and one for each top-level term's child terms. Then you use conditional groups to show and hide these generic fields. When the Form is submitted, custom code will find out which term is selected and apply that term to the post as a taxonomy term. Here's another ticket that discusses this approach: https://toolset.com/forums/topic/parent-child-term-taxonomies-on-cred-and-views/

Here's another example showing more details about the wp_set_object_terms method:
https://toolset.com/forums/topic/display-taxonomy-associated-with-port-parent/

Here are the API documents you can use as a reference:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_set_object_terms

I can help if you get stuck on any step, or need additional information.

#1169824
Screen Shot 2018-12-19 at 11.28.56 AM.png
Screen Shot 2018-12-19 at 11.29.10 AM.png
Screen Shot 2018-12-19 at 11.29.26 AM.png

Thank you Christian.

I have set up the top-level terms and two top-level term's child terms to test. I reviewed the samples you shared and set a conditional group to show/hide. The first child term shows, with nothing selected from the top-level term, the second child term does not show. See Code below and examples of the front-end form.

 
/**
 * Business Category
 */
[cred_generic_field type='select' field='biz-category' class='biz-category']
{
"required":0,
"default":[],
"options":[{"value":"animal-care","label":"Animal Care"},
      {"value":"arts-literature","label":"Arts & Literature"},
      {"value":"beauty-spa","label":"Beauty & Spa"},
      {"value":"care-coordinator","label":"Care Coordinator"},
      {"value":"childrens-needs","label":"Children's Needs"},
      {"value":"diversity-multi-cultural","label":"Diversity/Multi-Cultural"},
      {"value":"education","label":"Education"},
      {"value":"fashion","label":"Fashion"},
      {"value":"food-drink","label":"Food & Drink"},
      {"value":"goods-retail","label":"Goods & Retail"},
      {"value":"health-wellness","label":"Health & Wellness"},
      {"value":"home-garden","label":"Home & Garden"},
      {"value":"non-profit","label":"Non-Profit"},
      {"value":"play-venues","label":"Play Venues"},
      {"value":"professional-services","label":"Professional Services"},
      {"value":"real-estate","label":"Real Estate"},
      {"value":"technical-services","label":"Technical Services"},
      {"value":"travel","label":"Travel"},{"value":"venue","label":"Venue"}]
}
/**
 * Animal Sub-Category
 */
[/cred_generic_field]
[cred_generic_field type='select' field='subcat-animal' class='subcat-animal']
{
"required":0,
"default":[],
"options":[
      {"value":"dog-walker","label":"Dog Walker"},
      {"value":"pet-caretaking","label":"Pet Caretaking"},
      {"value":"pet-grooming","label":"Pet Grooming"},
      {"value":"pet-supplies","label":"Pet Supplies"},
      {"value":"veterinarian-services","label":"Veterinarian Services"},
      {"value":"training-services","label":"Training Services"},
      {"value":"alternative-cremation","label":"Alternative Cremation"}]
}
/**
 * Animal Conditional
 */
[/cred_generic_field]
[cred_show_group if="($(biz-category) eq  '14' )"  mode='fade-slide']
    <div class="form-group">
        <label>Sub-Category</label>
        [cred_field field='subcat-animal' force_type='field' select_text='--- not set ---' class='form-control' output='bootstrap']
    </div>
[/cred_show_group]
/**
 * Arts & Literature Sub-Category
 */      
[cred_generic_field type='select' field='arts-literature' class='arts-literature']
{
"required":0,
"default":[],
"options":["options":[
{"value":"actor-actress","label":"Actor / Actress"},
{"value":"animation","label":"Animation"},
{"value":"architecture","label":"Architecture"},
{"value":"author-publishing-services","label":"Author Publishing Services"},
{"value":"band","label":"Band"},
{"value":"cartoon-comic","label":"Cartoon / Comic"},
{"value":"ceramics","label":"Ceramics"},
{"value":"composer","label":"Composer"},
{"value":"custom-furniture","label":"Custom Furniture"},
{"value":"dancer","label":"Dancer"},
{"value":"editor","label":"Editor"},
{"value":"fashion-design","label":"Fashion Design"},
{"value":"film-cinema","label":"Film / Cinema"},
{"value":"film-production","label":"Film Production"},
{"value":"glassblowing","label":"Glassblowing"},
{"value":"graphic-art","label":"Graphic Art"},
{"value":"journalism","label":"Journalism"},
{"value":"literary-agent","label":"Literary Agent"},
{"value":"mixed-multi-media","label":"Mixed / Multi Media"},
{"value":"musician","label":"Musician"},
{"value":"painting-drawing","label":"Painting / Drawing"},
{"value":"photography","label":"Photography"},
{"value":"publishing","label":"Publishing"},
{"value":"pottery","label":"Pottery"},
{"value":"sculpting","label":"Sculpting"},
{"value":"singer-vocal-artist","label":"Singer / Vocal Artist"},
{"value":"tattoo-artist","label":"Tattoo Artist"},
{"value":"theater-production","label":"Theater Production"},
{"value":"video","label":"Video"}
{"value":"woodwork","label":"Woodwork"},
{"value":"writing-poetry","label":"Writing / Poetry"}]
}
[/cred_generic_field]
/**
 * Arts & Literature Conditional
 */    
[cred_show_group if="($(biz-category) eq  '15' )"  mode='fade-slide']
    <div class="form-group">
        <label>Sub-Category</label>
        [cred_field field='arts-literature' force_type='field' select_text='--- not set ---' class='form-control' output='bootstrap']
    </div>
[/cred_show_group]
#1170013

The first child term shows, with nothing selected from the top-level term, the second child term does not show.
1. Okay it looks like you have a cred_field for subcat-animal as well as a generic field for subcat-animal. Remember the generic fields should be used instead of the cred_fields. I think you should replace the cred_field for subcat-animal with the cred_generic_field for subcat-animal. Ditto for the arts-literature cred_field. Replace the cred_field with the cred_generic_field.
2. Your generic field values will never be "12" or "15", they will correspond to the values in your generic field options. Examples are "care-coordinator" or "sculpting" or "pet-supplies". So you should change the cred_show_group conditions to check for those specific values, not numbers like 12 or 15.

I hope this helps you get things set up.

#1170278

Thank Christian.
It's so close. Sub-categories don't show up once I add the conditional group. I have updated all to cred_generic_field. Your help would be greatly appreciated. I have to wrap this up before the weekend. I'm just not seeing what the issue could be. This is SO NOT my zone of genius!

[php]
<div class="profile-biz-cat-sub">
[cred_generic_field field='biz-category' type='select' class='form-control' output="bootstrap"]
{
"required":0,
"default":[],
"options":[{"value":"animal-care","label":"Animal Care"},
{"value":"arts-literature","label":"Arts & Literature"},
{"value":"beauty-spa","label":"Beauty & Spa"},
{"value":"care-coordinator","label":"Care Coordinator"},
{"value":"childrens-needs","label":"Children's Needs"},
{"value":"diversity-multi-cultural","label":"Diversity/Multi-Cultural"},
{"value":"education","label":"Education"},
{"value":"fashion","label":"Fashion"},
{"value":"food-drink","label":"Food & Drink"},
{"value":"goods-retail","label":"Goods & Retail"},
{"value":"health-wellness","label":"Health & Wellness"},
{"value":"home-garden","label":"Home & Garden"},
{"value":"non-profit","label":"Non-Profit"},
{"value":"play-venues","label":"Play Venues"},
{"value":"professional-services","label":"Professional Services"},
{"value":"real-estate","label":"Real Estate"},
{"value":"technical-services","label":"Technical Services"},
{"value":"travel","label":"Travel"},{"value":"venue","label":"Venue"}]
}[/cred_generic_field]
</div>
[cred_show_group if="( $(biz-category) eq 'animal-care' )" mode="fade-slide"]
<div class="profile-biz-cat-sub">
<label>Sub-Category:</label>
[cred_generic_field field='animal-care' type='select' class='form-control' output="bootstrap"]
{
"required":0,
"default":[],
"options":[{"value":"dog-walker","label":"Dog Walker"},
{"value":"pet-caretaking","label":"Pet Caretaking"},
{"value":"pet-grooming","label":"Pet Grooming"},
{"value":"pet-supplies","label":"Pet Supplies"},
{"value":"veterinarian-services","label":"Veterinarian Services"},
{"value":"training-services","label":"Training Services"},
{"value":"alternative-cremation","label":"Alternative Cremation"}]
}[/cred_generic_field]
</div>
[/cred_show_group]

[cred_show_group if="( $(biz-category) eq 'arts-literature' )" mode="fade-slide"]
<div class="profile-biz-cat-sub">
<label>Sub-Category:</label>
[cred_generic_field field='arts-literature' type='select' class='form-control' output="bootstrap"]
{
"required":0,
"default":[],
"options":[{"options":[{"value":"actor-actress","label":"Actor / Actress"},
{"value":"animation","label":"Animation"},
{"value":"architecture","label":"Architecture"},
{"value":"author-publishing-services","label":"Author Publishing Services"},
{"value":"band","label":"Band"},
{"value":"cartoon-comic","label":"Cartoon / Comic"},
{"value":"ceramics","label":"Ceramics"},
{"value":"composer","label":"Composer"},
{"value":"custom-furniture","label":"Custom Furniture"},
{"value":"dancer","label":"Dancer"},
{"value":"editor","label":"Editor"},
{"value":"fashion-design","label":"Fashion Design"},
{"value":"film-cinema","label":"Film / Cinema"},
{"value":"film-production","label":"Film Production"},
{"value":"glassblowing","label":"Glassblowing"},
{"value":"graphic-art","label":"Graphic Art"},
{"value":"journalism","label":"Journalism"},
{"value":"literary-agent","label":"Literary Agent"},
{"value":"mixed-multi-media","label":"Mixed / Multi Media"},
{"value":"musician","label":"Musician"},
{"value":"painting-drawing","label":"Painting / Drawing"},
{"value":"photography","label":"Photography"},
{"value":"publishing","label":"Publishing"},
{"value":"pottery","label":"Pottery"},
{"value":"sculpting","label":"Sculpting"},
{"value":"singer-vocal-artist","label":"Singer / Vocal Artist"},
{"value":"tattoo-artist","label":"Tattoo Artist"},
{"value":"theater-production","label":"Theater Production"},
{"value":"video","label":"Video"}
{"value":"woodwork","label":"Woodwork"},
{"value":"writing-poetry","label":"Writing / Poetry"}]
}[/cred_generic_field]
</div>
[/cred_show_group]
[php]

Thank you!
Laurie

#1170712

I'm not able to see the generic business category field here, and I still see the multiselect field: hidden link
Can I log in to wp-admin and see what's going on? Private reply fields are active here.

#1170760

I don't see anything obviously wrong with this code. I see that your Toolset plugins are a bit out of date, so please register and update to the latest versions. Clear your browser cache and test again. If that doesn't help, try temporarily deactivating all plugins except Types, Views, Maps and Forms. Test again. If the problem is resolved, reactivate your other plugins one by one until the problem returns. If the problem is not resolved, I'll need to make a copy of your site so I can run more tests on my local environment.

#1171258

Nothing is working. I was holding off activating the Toolset plugins as the site will move to eyeprosper.com when it goes live in 2 weeks, but figured it would be easier to mange. Everything is updated and backed up. I deactivated all the plugins and changed to twentyseventeen theme. still didn't work.

If you could take a look locally, I would greatly appreciate it. I have to have this all finished next week.

Thank you for all your help!
Laurie

#1171976

I'm not able to log in with the credentials you supplied before. Can you check and let me know if there's an updated login?

#1172308

Shane
Supporter

Languages: English (English )

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

Hello,

Christian is currently on Holiday at the moment and won't be available until Thursday.

Thank you for the patience.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.