Skip Navigation

[Resolved] Conditional select

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

Problem:
The issue here is that the user wanted to add a conditional for their various taxonomies and their children.

Solution:

This can be done by following the instructions in the link below.
https://toolset.com/forums/topic/conditional-select/#post-1229476

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

This topic contains 4 replies, has 2 voices.

Last updated by souhirB 5 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#1228651

Tell us what you are trying to do?
Hi
In add post form in front end in taxonomy i want to show conditional select field with sub categories depending in user choice in parent categorie:
Example When i choose for example Categorie 1 i will get a select field with all sub categories of category 1.
User can select more than 1 category parents and more than 1 subcategories
Thank you

#1228846

Shane
Supporter

Languages: English (English )

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

Hi Souhir,

Thank you for contacting our support forum.

This might be possible if you have a fixed number taxonomies and they won't change.

Is this how you have your taxonomies? If so then I can walk you through the steps on how to get this working for you.

Please let me know.

Thanks,
Shane

#1229049

Hi Shane
i have the basic categories of wordpress (8 parents categories & their subcategories
thank you

#1229476

Shane
Supporter

Languages: English (English )

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

Hi Souhir,

Thank you for the patience, this is actually going to take some custom work to do but it is still possible.

What you are going to have to do is to rewrite your taxonomy in the form as a cred generic field. Take a look at my example below.


[cred_generic_field type='select' field='parent_category']
{
"required":0,
"default":[],
"options":[{"value":"35","label":"Business"},{"value":"41","label":"Featured"},{"value":"29","label":"World"}]
}
[/cred_generic_field]

Notice that I created a custom select field using the Cred Generic field option and re-added my taxonomy to it. So for the Labels i use the taxonomy name and for the value I use the taxonomy ID. Now since you want the ID you can get it by going to the taxonomy edit page and clicking on the term you want the ID for and in the URL you should see the Tag_ID parameter.

Now I named my field parent_category for simplicity.

Next we need to separate the child terms. So for each parent category you will need to create a separate generic list for the child. Example.


[cred_show_group if="( $(parent_category) eq '35' )" mode="fade-slide"]
[cred_generic_field type='select' field='child_category2']
{
"required":0,
"default":[],
"options":[{"value":"37","label":"Finance"},{"value":"36","label":"Management"}]
}
[/cred_generic_field]
[/cred_show_group]

[cred_show_group if="( $(parent_category) eq '29' )" mode="fade-slide"]
[cred_generic_field type='select' field='child_category2']
{
"required":0,
"default":[],
"options":[{"value":"37","label":"Finance"},{"value":"36","label":"Management"}]
}
[/cred_generic_field][/cred_show_group]


If you also notice that around each field group you will see that i've added the conditional for the groups so they only show based on what parent was selected.

Now to make all this work you will need the code below.

add_action('cred_save_data', 'cust_set_cat',10,2);
function cust_set_cat($post_id, $form_data){
    if ($form_data['id']==602){
  	if($_POST['parent_category']==35){
           wp_set_post_terms( $post_id, $_POST['parent_category'], 'category' );
           if(isset($_POST['child_category1'])){
            wp_set_post_terms( $post_id, $_POST['child_category1'], 'category', true );
          }
        }
     if($_POST['parent_category']==29){
           wp_set_post_terms( $post_id, $_POST['parent_category'], 'category' );
           if(isset($_POST['child_category2'])){
            wp_set_post_terms( $post_id, $_POST['child_category2'], 'category', true );
          }
        }
    }
}

You will notice that I have set my first condition to 602 and this is because I only want it to work for only the Form with ID 602. The other if statements correspond to their respective terms.

It should be noted that you will need an if statement for each parent category and child category.

Please let me know if this example was clear.

Thanks,
Shane

#1268703

thank you