Skip Navigation

[Resolved] Select parent taxonomies automatically with Forms (CRED) post form

This support ticket is created 5 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
- 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 4 replies, has 2 voices.

Last updated by martinH-10 5 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#1287361
auto-tax.png

I am trying to automatically select all parent taxonomies of selected taxonomy when user save the post with cred form.

For example, I have

Czech republic > South Bohemian region > Budweis

When user select only City - Budweis, is it possible to select all parents automatically?

#1287377

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

You should use the Toolset Form's hook cred_save_data to automatically select the parent terms:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

For example - please try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_action('cred_save_data', 'func_add_parent_terms_automatically',10,2);
function func_add_parent_terms_automatically($post_id, $form_data){
    // if a specific form
    if ($form_data['id']==9999){

   $terms = wp_get_object_terms( $post_id,  'taxonomy-slug' );
     foreach($terms as $term){
        while($term->parent != 0 && !has_term( $term->parent, 'taxonomy-slug', $post_id)){
            // move upward until we get to 0 level terms
            wp_set_post_terms($post_id, array($term->parent), 'taxonomy-slug', true);
            $term = get_term($term->parent, 'taxonomy-slug');
        }
    }
        
    }
}

Where:
- Replace 9999 with your original form ID
- Replace taxonomy-slug with your original taxonomy slug

#1287539

Hello, thanks for reply. Code posted above works only for first parent. Not till 0 zero level.

What should I change ?

#1287543

Minesh
Supporter

Languages: English (English )

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

I just show you the way - you need to adjust the code accordingly where needed.

If you do not able to do so, I need a problem URL and access details and I will try to adjust the code accordingly.

*** 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 have set the next reply to private which means only you and I have access to it.

#1287775

My issue is resolved now. Thank you!