Skip Navigation

[Resolved] Conditionally update field value dependent on other field value using PHP

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

Problem:
Conditionally update field value dependent on other field value using PHP

Solution:
User was using the wrong custom field slug due to that the value was not fetched within the Toolset form's hook cred_save_data.

You can find proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/conditionally-update-field-value-dependent-on-other-field-value-using-php/#post-2069753

Relevant Documentation:

This support ticket is created 3 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.

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by jamesR-13 3 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#2069165

Tell us what you are trying to do?

On form submit, I want to check the value of one field in a custom post type and update another field's value on the same post with a different value, depending on what that value of the checked field is.

For example, on form submit, if "request type" value is "PTO", I want to set the value of "status" to "Department Head Approved" but if "request type" is any other value, I want to set "status" to "Approved" - using PHP.

What is the link to your site?
I can provide link and credentials in a private response if needed.

#2069233

Here's what I have so far:


add_action('cred_save_data_2017', 'save_data_for_form_with_id_2017',10,2);
function save_data_for_form_with_id_2017($post_id, $form_data)
{
	$req_type = get_post_meta($post_id, '_wpcf_type_of_request', true);
	if($req_type == 'PTO'){    
		update_post_meta($post_id, 'wpcf-status', 'Processing');
		update_post_meta($post_id, 'wpcf-sub-status', 'Command/Department Head Approved');
	}
	else{
    	update_post_meta($post_id, 'wpcf-status', 'Approved');
		update_post_meta($post_id, 'wpcf-sub-status', NULL);
	}
}

Unfortunately, the check on the 'type-of-request' field is never found to be true, even when the value is equal to 'PTO' in the post. I'm sure this is some stupid little syntax detail i'm overlooking, but I don't know what it is. Any help is appreciated.

#2069357

On further testing, it appears that my $req-type variable is not being populated by my call to get_post_meta()

Please advise.

#2069753

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL where you added the form and where you created the field "_wpcf_type_of_request"?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2070061

My issue is resolved now. Thank you!

I replaced "_wpcf_type_of_request" with ”wpcf-type-of-request” and my code worked. I was following very old forum posts that showed the underscores.