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
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.
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/