Home › Toolset Professional Support › [Resolved] Change child when we change parent
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 |
---|---|---|---|---|---|---|
- | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | - |
- | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | - |
Supporter timezone: Europe/London (GMT+00:00)
Tagged: Custom search, Views plugin
Related documentation:
This topic contains 13 replies, has 2 voices.
Last updated by Franck 5 years, 9 months ago.
Assisted by: Nigel.
Hello,
I use relationship field. And i'm wondering about the post updating.
When i update a post that it has child with relation field, how i can update all fields in the same time ?
Thanks for your answer.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
I'm not exactly sure what you are aiming for, can I get some more details?
Are you talking about updating posts on the front end or back end?
Can you give specific examples of the post types and the fields so I can better understand what you want to do?
Hi,
So, i have two custom types. (just an example)
House and room
I have a field district that i have to associate with this two custom types because i have to filter on it.
My problem is when i want update the district on a house i want to update also on rooms associate with the house.
Is it a good example ?
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
OK, so when you save one post you want to duplicate a field on a related post.
That shouldn't be too difficult, but the answer depends on whether you are submitting posts in the back end or using a front-end form?
Both 🙁
But usually Front-End with CRED.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Sorry, can I clarify.
You asked about publishing a parent post and duplicating one of the fields to the linked child post, and said you would normally do this with a front-end form.
But with front-end forms you can include a parent-selector on a form to publish child posts, but not the other way around.
So it would be easy enough with a front-end form to publish child posts and duplicate a field to the parent specified in the form.
If you have a front-end form to publish parent posts there is no child selector in the form, so what would be your intention, then, because if the parent has just been published, it will have no children to copy the field to.
If you could please clarify the workflow, then I can help with the code.
Hi, i'll try to clarify
--- I don't want copy field, i want copy the content of the parent field, but the problem is not when i publish, but when i edit.
First : i have to say, i just work with CRED.
Second : I make example.
I have house type and room type.
I create house post, ok, i create room post ok. Until here i am ok.
I have created custom code to fill the relationship field on the child post (room). So when i create a post, i just have to fill the relationship field on the parent post and for the child post it is automatic.
As you can see there : hidden link
MY PROBLEM IS WHEN I UPDATE THE POST (parent post)
Because the relationship field have to be updated when we change parent post, i would like solution to update child relationship fields when we update parent post.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
You are saying "relationship field" but I'm not sure you mean "relationship field".
Relationship fields are a concept related to many-to-many relationships, and yours is a one-to-many (parent-child) relationship.
So, I'm going to assume that
- you have a published parent post
- it is already associated with published child posts
- the parent post has a custom field which should be copied to the child posts whenever the parent post is edited by a CRED form
Here is an example of code you could use to achieve this which uses the cred_save_data hook:
/** * Update child posts with field from edited parent post */ add_action( 'cred_save_data', 'tssupp_form_submit', 10, 3 ); function tssupp_form_submit( $post_id, $form_data ){ // Edit these as required $relationship_slug = 'project-task'; $custom_field_key = 'wpcf-due'; // note wpcf- prefix for Types fields if ( in_array( $form_data['id'], array( 243 ) ) ) { // Edit for form ID // get child posts of this parent $children = toolset_get_related_posts( $post_id, $relationship_slug, array( 'query_by_role' => 'parent', 'limit' => 999, 'return' => 'post_id', 'role_to_return' => 'child' ) ); // get the parent custom field value $field_value = get_post_meta( $post_id, $custom_field_key, true ); // update the children with this field value foreach ($children as $key => $child) { update_post_meta( $child, $custom_field_key, $field_value ); } } }
In my example it is a relationship between projects and child tasks, and whenever the parent project is edited, the due date is copied to the child tasks. You would need to edit the relationship slug, custom field key, and edit form ID.
Does that do what you need?
Hi,
You're right, it is a child post.
Your solution is almost perfect, but with this way we can not change post by admin.
So i have replaced add_action( 'cred_save_data', 'tssupp_form_submit', 10, 3 );
By
add_action( 'post_updated', 'tssupp_form_submit', 10, 3 );
and if ( in_array( $form_data['id'], array( 243 ) ) )
BY
if ( $form_data->post_type == 'immobilier-neuf' )
But as i understand i have to make 2 hooks : yours and mine ? One for CRED and the other for ADMIN UPDATE ?
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
You could use the post_updated action, although I would use the save_post action.
You are doing something similar, but you are not updating the names of the function arguments, which confused me for a while when I first looked at your sample code.
Here is the code I would suggest:
/** * Update child posts with field from edited parent post */ add_action( 'save_post', 'tssupp_form_submit', 10, 3 ); function tssupp_form_submit( $post_id, $post, $update ){ // Edit these as required $relationship_slug = 'project-task'; $custom_field_key = 'wpcf-due'; // note wpcf- prefix for Types fields if ( $update && $post->post_type == 'immobilier-neuf' ) { // get child posts of this parent $children = toolset_get_related_posts( $post_id, $relationship_slug, array( 'query_by_role' => 'parent', 'limit' => 999, 'return' => 'post_id', 'role_to_return' => 'child' ) ); // get the parent custom field value $field_value = get_post_meta( $post_id, $custom_field_key, true ); // update the children with this field value foreach ($children as $key => $child) { update_post_meta( $child, $custom_field_key, $field_value ); } } }
Thanks for your answer.
It is not good because the data that we get is not updated yet.
So when we call get_post_meta( $post_id, $custom_field_key, true );
The data is the old data.
I try lot of things but it doesn't work.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
OK, let me take another look, because the save_post hook fires *after* the post has been updated, so it should work, but I'll test on my own site to check.
You are testing editing posts on the back end or with a front-end Form?
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
I found that the code worked fine when editing posts from the back end, but not when editing with a front-end Form, where at the time save_post (or post_updated) action runs the post meta data has not been updated yet.
So, if you need it to work for both front-end and back-end editing, we need to modify the code and test if we are submitting via a front-end form, and if so, get the updated field value from the global $_POST object.
So my updated code looks like this:
/** * Update child posts with field from edited parent post */ add_action('save_post', 'tssupp_form_submit', 10, 3); function tssupp_form_submit($post_id, $post, $update) { // Edit these as required $relationship_slug = 'project-task'; $custom_field_key = 'wpcf-due'; if ($post->post_type == 'project') { // get child posts of this parent $children = toolset_get_related_posts( $post_id, $relationship_slug, array( 'query_by_role' => 'parent', 'limit' => 999, 'return' => 'post_id', 'role_to_return' => 'child', ) ); // get the parent custom field value $field_value = get_post_meta($post_id, $custom_field_key, true); // if submitted via CRED form this needs overwriting if (isset($_POST['_cred_cred_prefix_post_id'])) { $field_value = $_POST[$custom_field_key]['datepicker']; } // update the children with this field value foreach ($children as $key => $child) { update_post_meta($child, $custom_field_key, $field_value); } } }
There is one complication, namely that different field types populate $_POST a little differently.
So for a simple field such as a single line text field you can get the value directly, e.g. with $_POST['wpcf-my-field'].
In my sample code my custom field is a date field, and I would get the value with $_POST['wpcf-my-date']['datepicker'].
I'll leave it to you to determine if you can use the first simple case, depending on your field type.
My issue is resolved now. Thank you!
IT is perfect.