Tell us what you are trying to do? I have form containing categories in dropdown on it. i am trying to save all parent categories also if child category is selected.
Example:
Samsung----Refrigerator----LG----200L
ABC----XYZ----123
Now if user select 200L then 200L subcategory will be saved in post. I want to save all these 4 in post.
Now if user select 123 then I want to save all these 3 (ABC, XYZ, 123) in post.
Is there any documentation that you are following?No
Is there a similar example that we can see?No
What is the link to your site?
Thanks
Hi,
Thank you for contacting us and I'd be happy to assist.
This will require a custom function attached to the form hook, "cred_save_data":
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
For example:
add_action('cred_save_data', 'custom_add_parent_terms_action',10,2);
function custom_add_parent_terms_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12345)
{
// target taxonomy slug
$taxonomy = 'taxonomy-slug';
// get the attached term
$term_list = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) );
if(!empty($term_list)) {
// get all the parent terms and set them to the post
foreach ($term_list as $term_item) {
$parents = get_ancestors( $term_item, $taxonomy );
if(!empty($term_list)) {
wp_set_post_terms( $post_id, $parents, $taxonomy, true );
}
}
}
}
}
Note: You'll replace '12345' and 'taxonomy-slug' with your website's actual target form ID and the taxonomy's slug.
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
When the form will be submitted, this function will get the attached taxonomy term and will then attach all its parent terms too.
I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
My issue is resolved now. Thank you!