Skip Navigation

[Resolved] Update Parent Field when submitting child post form

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

Problem: I have a Form that creates new child posts. I would like to use the cred_save_data API and the new Post Relationships API to update a custom field value in the parent post. I also have a Form that edits a parent post. I would like to use the APIs to update custom field values in all its child posts.

Solution:
To update the parent from the child post Form:

function consultant_data_action($post_id, $form_data) {
     
if ($form_data['id']==30) {
  
$consultant = toolset_get_related_post( $post_id, 'relationship-slug', 'parent' );
  
$update = 500;
update_post_meta($consultant, 'wpcf-updated-consultant-field', $update);
}
}
add_action('cred_save_data', 'consultant_data_action',10,2);

To update all children from the parent post Form:

function reverse_consultant_data_action($post_id, $form_data) {
        
if ($form_data['id']==23) {
$rels = toolset_get_related_posts(
    $post_id,
    'consultant-job',
    'parent',
    1000000,
    0,
    array(),
    'post_id',
    'child'
  );
  foreach($rels as $rel) {
    update_post_meta($rel, 'wpcf-update-me', 500);
  }
}}
add_action('cred_save_data', 'reverse_consultant_data_action',10,2);

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 years, 5 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
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)

This topic contains 9 replies, has 2 voices.

Last updated by ioannisM-2 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#922139

I have created the post type consultant and the child post type job. I need when a new job post is created the field wpcf-updated-consultant-field from the consultant post been updated with specific value.

I try to follow this documentation: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_parent_post_by_type

Here is my cred save data hook:

function consultant_data_action($post_id, $form_data) {
 
if ($form_data['id']==30) {
	
$consultant = get_post( $consultant_id ); $job = toolset_get_parent_post_by_type( $consultant, 'job' );	
$update = 500;
update_post_meta($consultant, 'wpcf-updated-consultant-field', $update);
}
}
add_action('cred_save_data', 'consultant_data_action',10,2);

but it is not working... What am I doing wrong?

#922276

Hi, please note that the toolset_get_parent_post_by_type API can only be used if the post relationship was created in the old (legacy) system. Post relationships created in Types 3.0+ or after migrating to new Post Relationships will not work with this API, you should use toolset_get_related_posts instead. If you are working with a legacy relationship or a migrated legacy relationship, this API is okay.

It looks like you've got a couple of issues in this code. The first issue is there is no variable $consultant_id so that's going to throw a fatal error. Second, it looks like you have your child and parent post types backwards. Assuming this is a legacy relationship or migrated legacy relationship, try this update:

function consultant_data_action($post_id, $form_data) {
  
if ($form_data['id']==30) {
     
$consultant = toolset_get_parent_post_by_type( $post_id, 'consultant' ); 
$update = 500;
update_post_meta($consultant, 'wpcf-updated-consultant-field', $update);
}
}
add_action('cred_save_data', 'consultant_data_action',10,2);
#922281

Thank you Christian for your reply! I tried your code and also I tried:

function consultant_data_action($post_id, $form_data) {
   
if ($form_data['id']==30) {

$consultant = toolset_get_related_post( $job_id, array( 'consultant', 'job' ) );

$update = 500;
update_post_meta($consultant, 'wpcf-updated-consultant-field', $update);
}
}
add_action('cred_save_data', 'consultant_data_action',10,2);

but neither is working. How could I get the parent id and update a parent's custom field with the new relationships system each time I am creating a new child post? Could you give me an example?

#922643

For relationships created in the new system, the code is like this:

function consultant_data_action($post_id, $form_data) {
    
if ($form_data['id']==30) {
 
$consultant = toolset_get_related_post( $post_id, 'relationship-slug', 'parent' );
 
$update = 500;
update_post_meta($consultant, 'wpcf-updated-consultant-field', $update);
}
}
add_action('cred_save_data', 'consultant_data_action',10,2);

You must replace 'relationship-slug' with the correct relationship slug, which can be found by editing the post relationship in Toolset > Relationships. While you're there, you can see a list of all relationships. If the relationship was migrated from a legacy relationship, it will say "Migrated".

#924206

Christian I am so sorry but I cannot make it work. If the child post type is job and the parent post type is consultant my code is:

function consultant_data_action($post_id, $form_data) {
     
if ($form_data['id']==30) {
  
$consultant = toolset_get_related_post( $post_id, 'consultant-job', 'parent' );
  
$update = 500;
update_post_meta($consultant, 'wpcf-updated-consultant-field', $update);
}
}
add_action('cred_save_data', 'consultant_data_action',10,2);

I expect the consultant's custom field 'wpcf-updated-consultant-field' to take the value 500 after the submition of the form with the id 30 but nothing happens... I also tried replacing in the code "parent" with "consultant" but again no result... I do not understand what I am doing wrong...

#924233

I was just editing wrong functions.php file... Now it works like a charm. I can change the parent post's field when creating a child post. I tried also do the opposite but it didn't work. If we assume that a consultant has many jobs child posts and each job has the field wpcf-update-job-field, how could update this field of every job and give it the value 500 if I edit the consultant?

#924364

You can perform the child post update from the parent post by querying all the related child posts, then looping over them and updating their postmeta values. Something like this:

function reverse_consultant_data_action($post_id, $form_data) {
     
if ($form_data['id']==12345) {
$rels = toolset_get_related_posts(
    $post_id,
    'consultant-job',
    'parent',
    1000000,
    0,
    array(),
    'post_id',
    'child'
  );
  foreach($rels as $rel) {
    update_post_meta($rel, 'wpcf-child-field', 500);
  }
}
add_action('cred_save_data', 'reverse_consultant_data_action',10,2);
#924681

Hello Cristian,

Here is my updated code but it does not work...

function reverse_consultant_data_action($post_id, $form_data) {
      
if ($form_data['id']==23) {
$rels = toolset_get_related_posts(
    $post_id,
    'consultant-job',
    'parent',
    1000000,
    0,
    array(),
    'post_id',
    'child'
  );
  foreach($thisRel as $rel) {
    update_post_meta($rel, 'wpcf-update-me', 500);
  }
}}
add_action('cred_save_data', 'reverse_consultant_data_action',10,2);

The relationship slug and the custom field slug are correct, why might not be working?
Also I would like to ask you what is the number after 'parent'

Thank you

#947818

There was a typo in my code, sorry. Here's an updated version:

function reverse_consultant_data_action($post_id, $form_data) {
       
if ($form_data['id']==23) {
$rels = toolset_get_related_posts(
    $post_id,
    'consultant-job',
    'parent',
    1000000,
    0,
    array(),
    'post_id',
    'child'
  );
  foreach($rels as $rel) {
    update_post_meta($rel, 'wpcf-update-me', 500);
  }
}}
add_action('cred_save_data', 'reverse_consultant_data_action',10,2);

The number after "parent" is the maximum number of child posts to return. There is currently no way to tell the toolset_get_related_posts API to return all the results with no limit - a positive integer value must be supplied here. It's not ideal, and there is discussion going on about this among the team. I'm hoping this gets modified soon to behave more like a standard post query. For now, you must choose a number.

#952281

Now it works like a charm, thank you so much Cristian!!!