Skip Navigation

[Resolved] Edit parent post Taxonomy terms from child post CRED

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

Problem: I would like to modify the terms associated with a parent post when a child post is added or modified using CRED.

Solution: Use the cred_save_data hook and wp_set_object_terms to manipulate the parent post.

add_action('cred_save_data_12345', 'set_parent_tutor_term_from_child',100,2);
 
function set_parent_tutor_term_from_child($post_id, $form_data) {
  $taxonomy = 'tutor-status';
  $tag = array( 'tutor-term-slug' );
  $parent_id = get_post_meta($postid, '_wpcf_belongs_tutorslug_id', true);
  wp_set_object_terms( $parent_id, $tag, $taxonomy, true );
}

Change 12345 to match your CRED form ID, change tutor-status to match your Tutor Status taxonomy slug, change tutor-term-slug to match the term slug you want to associate with the parent post, and change tutorslug to match your tutor post type slug.

Relevant Documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_set_object_terms

This support ticket is created 7 years, 2 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#570519

Tell us what you are trying to do?
In the system I am designing with Toolset Types, I have these custom types
Tutors
Students
Matches

Tutor contains a number of fields about Tutors and also uses an item that is a taxonomy named "Tutor Status"
Student contains a number of fields about Students and also uses an item that is a taxonomy named "Student Status"
Matches is a child post type of Tutor and Student and contains information that ties a Tutor to a Student.

On my Add and Edit Match forms, I want to update the Student->Student Status and Tutor->Tutor Status values. From the documentation, I don't see how to access a taxonomy associated with a parent post. Can you point me to an explanation?

Is there any documentation that you are following?
I have not found any.

Is there a similar example that we can see?
No

What is the link to your site?
hidden link <- I can give you temporary access.

#570754

Hi, a CRED form is designed to only create or edit one post at a time. So there's not a straightforward way to modify a parent post's taxonomy terms when you create a child post. It's necessary to use the cred_save_data hook to programmatically modify a parent post using custom code with the native WordPress API wp_set_object_terms. We have information about the cred_save_data hook here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

WP's documentation for the wp_set_object_terms API:
https://codex.wordpress.org/Function_Reference/wp_set_object_terms

A basic example:

add_action('cred_save_data_12345', 'set_parent_tutor_term_from_child',100,2);

function set_parent_tutor_term_from_child($post_id, $form_data) {
  $taxonomy = 'tutor-status';
  $tag = array( 'tutor-term-slug' );
  $parent_id = get_post_meta($postid, '_wpcf_belongs_tutorslug_id', true);
  wp_set_object_terms( $parent_id, $tag, $taxonomy, true );
}

This will associate the term 'tutor-term-slug' from the taxonomy 'tutor-status' to the parent tutor of the post just created by CRED.Change 12345 to match your CRED form ID, change tutor-status to match your Tutor Status taxonomy slug, change tutor-term-slug to match the term slug you want to associate with the parent post, and change tutorslug to match your tutor post type slug.

You can reuse this code, with a few minor adjustments, for the student term as well. If you want to allow the user to select between multiple terms in the CRED form, it requires a bit more work because you must create the term select input, and capture that value from the form submission object.

Let me know if you have additional questions about this, and I can provide further assistance if necessary.

#571152

Thanks! I appreciate your detailed response and the example code.