Skip Navigation

[Resolved] Automatically change a field in one type when a field in another type is changed

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

Problem:
How to update another Custom field when a there is as given value in a custom field of current edited post

Solution:
1. use a cred_save_data action

2. Check if the Field's value is == value

3. If this is true do your custom actions.

4. This is a example code:

function position_filled($post_id, $form_data) 
{
    // if a specific form
    if ($form_data['id']==168) //CRED Form ID
    {
  
       //get value of cv-status (Radio value of current CV Sent edited with CRED)
       $new_cv_status = get_post_meta($post_id, 'wpcf-value', true);
    
       //check if CV Status == 'Placed'
       if ($new_cv_status == 7) 
       {
   
          //get ID of parent Job , which is stored in the CV Status wpcf_belongs_job_id
          $ID = get_post_meta($post_id, '_wpcf_belongs_parent-file_id', true);
   
          //get the parent post object
          $post_parent_object = get_post( $ID ); 
   
          //get it's ID
          $ID_parent = $post_parent_object->ID;
  
           //Update the parent's post field (RADIO)
           update_post_meta($ID_parent, 'wpcf-value', 1);
  
         }
      }
}
  
add_action('cred_save_data', 'position_filled',10,2);

Relevant Documentation:
https://codex.wordpress.org/Function_Reference/update_post_meta
https://developer.wordpress.org/reference/functions/get_post_meta/
https://codex.wordpress.org/Template_Tags/get_posts
https://toolset.com/documentation/user-guides/cred-api/#csd

This support ticket is created 8 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by gavinS 8 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#358819

I have a type called 'Jobs' with a field called 'Job Status' and another type called 'CV Sent' (a child of Job) with a field called 'CV Status'. Both status fields are radio fields.

How do I automatically change the value of the parents 'Job Status' to 3 when 'CV Status' is changed to 7.

#358861

You will need a custom CRED API function.

In this case you can use the cred_save_data:
https://toolset.com/documentation/user-guides/cred-api/#csd

Apply it on the form that edits "CV Status"

In it, query the parent Posts of the current post edited.

This is done by using a get_posts() query, where you get the Posts of Type "Jobs" where the Custom Field _wpcf_belongs_{parent-slug}_id is equal to the Currently edited Post ID.

Then get_post_meta() the Custom Field "CV Status" and update_post_meta the Custom field "Job Status"

Relevant DOC:
https://codex.wordpress.org/Function_Reference/update_post_meta
https://developer.wordpress.org/reference/functions/get_post_meta/
https://codex.wordpress.org/Template_Tags/get_posts
https://toolset.com/documentation/user-guides/cred-api/#csd

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#360406

Hi Beda

Thanks, but are you sure that is right?

Job is the parent, so surely I need to be querying jobs whose ID is equal to the current posts wpcf_belongs_job_id?

Not posts of Type "Jobs" where the Custom Field _wpcf_belongs_{parent-slug}_id is equal to the Currently edited Post ID??

Thanks

Gavin

#360428

I apologize, I must have had a bad day on this (and your other) thread.

Of course, you will get the value of the currently edited CHILD posts _wpcf_belongs_{parent-slug}_id.
That will return ONE value (the ID), as you can have anyway only ONE parent per type each child post.

This allows you to get_post() (parent one, job) where you get the (only) one post object that matches the ID returned previously.

//get value of Field (your Radio of current Child Post you edit with CRED)
$value_of_field = get_post_meta($current_post, 'wpcf-yourfield, true);

//get ID of parent, which is stored in the Child's Field _wpcf_belongs_job_id
$ID = get_post_meta($current_post, '_wpcf_belongs_job_id', true);

//get the parent post object
$post_parent_object = get_post( $ID ); 

//get it's ID
$ID_parent = $post_parent_object->ID;

//Update the parent's post field (RADIO)
update_post_meta($ID_parent, 'wpcf-yourfield', $value_of_field),

If you need this action only if the Radio is 7, you need to use a if() statement.
Of course above code needs to be run in a cred_save_data function.

Please find here some related DOC:
https://developer.wordpress.org/reference/functions/get_post/
(the other previously submitted still are valid, unless the get_posts())

That should do it

#360448

Thanks man

I've tried the following, but the job status doesn't seem to be changing. Can you see anything wrong?


function position_filled($post_id, $form_data) 
{
    // if a specific form
    if ($form_data['id']==209) //CRED Form ID
    {

       //get value of cv-status (Radio value of current CV Sent edited with CRED)
       $new_cv_status = get_post_meta($post_id, 'wpcf-cv-status', true);
  
       //check if CV Status == 'Placed'
       if ($new_cv_status == 7) 
       {
 
          //get ID of parent Job , which is stored in the CV Status wpcf_belongs_job_id
          $ID = get_post_meta($post_id, 'wpcf_belongs_job_id', true);
 
          //get the parent post object
          $post_parent_object = get_post( $ID ); 
 
          //get it's ID
          $ID_parent = $post_parent_object->ID;

           //Update the parent's post field (RADIO)
           update_post_meta($ID_parent, 'wpcf-status', 3);

         }
      }
}

add_action('cred_save_data', 'position_filled',10,2);

#360657

Okay I see I left out the '_' before _wpcf_belongs_job_id in the previous code, but changed that and still not working.

Not really sure how the radio fields are saved? Are they numeric or strings?

#360674
Bildschirmfoto 2016-01-20 um 17.30.48.png

The below code works just perfect (it's a bit adapted to my own install) as long we update a single value.

function position_filled($post_id, $form_data) 
{
    // if a specific form
    if ($form_data['id']==168) //CRED Form ID
    {
 
       //get value of cv-status (Radio value of current CV Sent edited with CRED)
       $new_cv_status = get_post_meta($post_id, 'wpcf-value', true);
   
       //check if CV Status == 'Placed'
       if ($new_cv_status == 7) 
       {
  
          //get ID of parent Job , which is stored in the CV Status wpcf_belongs_job_id
          $ID = get_post_meta($post_id, '_wpcf_belongs_parent-file_id', true);
  
          //get the parent post object
          $post_parent_object = get_post( $ID ); 
  
          //get it's ID
          $ID_parent = $post_parent_object->ID;
 
           //Update the parent's post field (RADIO)
           update_post_meta($ID_parent, 'wpcf-value', 1);
 
         }
      }
}
 
add_action('cred_save_data', 'position_filled',10,2);

That works just fine as well with a Radio Field that has options.

The main problem here is, the code will get the VALUE and update the VALUE to the Database.

So, your Radio field, using above exact code, must be setup as in the screenshot.

Then it would work just fine, I tested that locally.

Of course you need to completely adapt the code to your slugs and CPT names.

Also, the CRED Edit form in my case sits directly in the Post that is been edited, so no link to the Form, but the form directly.

A parent is assigned already to the post (obviously) and the Form is set up to show the post after submit and publish the Post after submit.

#360949

Hi Beda

I just want to clarify. When a change is made to the radio button on one CRED form, I want to change a value of another radio field from a different type, which would have it's own seperate CRED form. Are you saying I need to also be displaying the other CRED form on the same page for this to work?

Or does that not matter as long as the form which we are currently editing exists on the page, as the changes are made to the database?

#360952

No, as I elaborate in the Code (comments), you are checking if the value of the currently submitted POST is 7.

If so, it updates the field of post Type B. (The parent of current edited/submitted post)

If not, it skips.

No additional CRED is needed.

You just submit the Form for Post A, and if 7 is true for that field, Post Type B is updated.

Here we are not dealing with a check of the FORM but of the POST submitted.

So the Form not even needs to be on the same page as the Post edited.

It can be anywhere.

It's not like in the other thread, where you need to check the FORM, because you use a cred_before_save_data action.

Here we use a cred_save_data

Thank you

#360953

Argh. Well I don't know what I did yesterday, and why it wasn't working, but I finally seem to have got it to work.

Thanks so much for all the help..

Gavin

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.