Skip Navigation

[Resolved] Parent-Child Term taxonomies on cred and views

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

Problem: I would like to show hierarchical taxonomies in CRED and in Views filters. At first, only the top-level terms should be shown. Then as the User selects terms, the child terms of those terms become visible.

Solution: There's not a simple way to accomplish this type of stepped interface in CRED or in Views. It may be possible to accomplish something similar in CRED using a dynamic system of generic fields and conditionals. I've helped another User set up something similar in this ticket: @https://toolset.com/forums/topic/categories-display/

The general idea is that you set up a generic field that contains each of the top-level terms in the taxonomy. Then you create separate generic fields that contain the subcategories of each top-level term. Show and hide the subcategory generic fields based on the selection in the top-level generic field using CRED conditionals. You can follow the example here that shows how to use a View to populate the options of a generic field dynamically: @https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms

However in Views, this approach is not possible because there are no generic field filters. Splitting up a hierarchical taxonomy search filter like this will require a significant amount of custom code using JavaScript and PHP, and unfortunately that falls outside the scope of the support we provide here in the forums. You may be able to find a skilled developer who can assist with that type of work in our contractors portal: @https://toolset.com/contractors

This support ticket is created 6 years, 7 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)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#627181
locations.png
locations-22.png

Tell us what you are trying to do?

I'm trying to show child terms according to parent term selected on cred forms and views.

Location is created as taxonomy because I want them as clickable.

I want chil

<div class="form-group">
		<label>Locations</label>
		[cred_field field='location' display='checkbox' output='bootstrap']
		[cred_field field='location_add_new' taxonomy='location' type='add_new']
	</div>

It still shown as checkboxes if I changed checkboxes to dropdown.

[cred_field field='location' display='dropdown' output='bootstrap']

Main problem is;

I got long list of locations which will be longer at future.
How can I show them in two step. Parent to child

Is there any documentation that you are following?

I looked for it at forum just only found this but it didn't help.
https://toolset.com/forums/topic/conditional-display-of-taxonomies-in-cred-form/

What is the link to your site?

hidden link

Thanks

#627479

Hi, there's not a simple way to accomplish this type of stepped interface in CRED or in Views. It may be possible to accomplish something similar in CRED using a dynamic system of generic fields and conditionals. I've helped another User set up something similar in this ticket: https://toolset.com/forums/topic/categories-display/

The general idea is that you set up a generic field that contains each of the top-level terms in the taxonomy. Then you create separate generic fields that contain the subcategories of each top-level term. Show and hide the subcategory generic fields based on the selection in the top-level generic field using CRED conditionals. You can follow the example here that shows how to use a View to populate the options of a generic field dynamically: https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms

However in Views, this approach is not possible because there are no generic field filters. Splitting up a hierarchical taxonomy search filter like this will require a significant amount of custom code using JavaScript and PHP, and unfortunately that falls outside the scope of the support we provide here in the forums. You may be able to find a skilled developer who can assist with that type of work in our contractors portal: https://toolset.com/contractors

Let me know if you have questions about the CRED approach and we can discuss in more detail.

#627624

Hi Christian , ok I forgot views option. Just focus cred part.
I've added generic fields.

 
	<div class="form-group">
		<label>State</label>
		[cred_generic_field field='States' type='select' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"options":[
{"value":"Alabama","label":"Alabama"},
{"value":"Indiana","label":"Indiana"},
{"value":"Louisiana","label":"Louisiana"},
{"value":"Nebraska","label":"Nebraska"},
{"value":"Texas","label":"Texas"},
{"value":"Utah","label":"Utah"}
]
}
[/cred_generic_field]

	</div>


[cred_show_group if="($(States) eq  'Alabama' )"  mode='fade-slide']

	<div class="form-group">
		<label>City</label>
		[cred_generic_field field='AlabamaCities' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":1,
"default":[],
"options":[
{"value":"Abbeville","label":"Abbeville"},
{"value":"Adamsville","label":"Adamsville"},
{"value":"Auburn","label":"Auburn"},
{"value":"Bayou La Batre","label":"Bayou La Batre"},
{"value":"Bessemer","label":"Bessemer"},
{"value":"Brent","label":"Brent"},
{"value":"Chickasaw","label":"Chickasaw"},
{"value":"Cordova","label":"Cordova"},
{"value":"Demopolis","label":"Demopolis"}
]
}
[/cred_generic_field]
      
      </div>
[/cred_show_group]
[cred_show_group if="($(States) eq  'Indiana' )"  mode='fade-slide']
<div class="form-group">
		<label>City</label>
      [cred_generic_field field='IndianaCities' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":1,
"default":[],
"options":[
{"value":"Carmel","label":"Carmel"},
{"value":"Connersville","label":"Connersville"},
{"value":"Elkhart","label":"Elkhart"},
{"value":"Fort Wayne","label":"Fort Wayne"},
{"value":"Franklin","label":"Franklin"},
{"value":"Michigan City","label":"Michigan City"}
]
}
[/cred_generic_field]
</div>
[/cred_show_group]
  [cred_show_group if="($(States) eq  'Louisiana' )"  mode='fade-slide']
  <div class="form-group">
		<label>City</label>
    [cred_generic_field field='LouisianaCities' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":1,
"default":[],
"options":[
{"value":"Baker","label":"Baker"},
{"value":"Bogalusa","label":"Bogalusa"},
{"value":"Bossier City","label":"Bossier City"},
{"value":"Carencro","label":"Carencro"},
{"value":"DeQuincy","label":"DeQuincy"},
{"value":"Eunice","label":"Eunice"},
{"value":"Harahan","label":"Harahan"}
]
}
[/cred_generic_field]

      </div>
[/cred_show_group]
[cred_show_group if="($(States) eq  'Nebraska' )"  mode='fade-slide']
 <div class="form-group">
		<label>City</label>
   [cred_generic_field field='NebraskaCities' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":1,
"default":[],
"options":[
{"value":"Cheyenne","label":"Cheyenne"},
{"value":"Geneva","label":"Geneva"},
{"value":"Merrick","label":"Merrick"},
{"value":"Nebraska City","label":"Nebraska City"},
{"value":"Sidney","label":"Sidney"}
]
}
[/cred_generic_field]

    </div>
[/cred_show_group]
[cred_show_group if="($(States) eq  'Texas' )"  mode='fade-slide']
<div class="form-group">
		<label>City</label>
  [cred_generic_field field='TexasCities' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":1,
"default":[],
"options":[
{"value":"Argyle","label":"Argyle"},
{"value":"Beach City","label":"Beach City"},
{"value":"Benavides","label":"Benavides"},
{"value":"Brazoria","label":"Brazoria"},
{"value":"Brazos Country","label":"Brazos Country"},
{"value":"Burkburnett","label":"Burkburnett"},
{"value":"Collin","label":"Collin"},
{"value":"Dallas","label":"Dallas"},
{"value":"Denton","label":"Denton"}
]
}
[/cred_generic_field]

  </div>
[/cred_show_group]
[cred_show_group if="($(States) eq  'Utah' )"  mode='fade-slide']
<div class="form-group">
		<label>City</label>
  [cred_generic_field field='UtahCities' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":1,
"default":[],
"options":[
{"value":"Bicknell","label":"Bicknell"},
{"value":"Cedar City","label":"Cedar City"},
{"value":"Charleston","label":"Charleston"},
{"value":"Clinton","label":"Clinton"},
{"value":"Draper","label":"Draper"},
{"value":"Ephraim","label":"Ephraim"},
{"value":"Garden City","label":"Garden City"},
{"value":"Garfield","label":"Garfield"},
{"value":"Henrieville","label":"Henrieville"},
{"value":"Salt Lake","label":"Salt Lake"}
]
}
[/cred_generic_field]

  </div>
   [/cred_show_group]

I've just found generic field training here but there's no knowledge for saving daata.
https://toolset.com/documentation/user-guides/cred-training-course/part-3-adding-generic-fields-conditional-groups-form/.

I tried to make a solution via https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==223)
    {
        if (isset($_POST['NebraskaCities']))
        {
            // add it to saved post meta
            add_post_meta($post_id, '__location', $_POST['location'], true);
        }
    }
}

And nothing.

Ok cred form is working without saving Generic fields to location taxonomy How can I save data to gadget post type

Thanks

#628067

Ok cred form is working without saving Generic fields to location taxonomy
I see in your cred_save_data hook, you are calling "add_post_meta". The add_post_meta function is used to add custom field values to a post, but I think you want to set post terms, right? These are different things. Instead of add_post_meta, you should probably call wp_set_object_terms as described here: https://codex.wordpress.org/Function_Reference/wp_set_object_terms

Tip - it will be much easeier if your generic field values match your taxonomy term slugs, like 'bayou-la-batre' instead of 'Bayou La Batre'.