Skip Navigation

[Resolved] How to save all parent categories through cred form.

This support ticket is created 2 years, 3 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

Author
Posts
#2441427

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

#2442297

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

#2442337

My issue is resolved now. Thank you!