Skip Navigation

[Resolved] Updating Parent Post based on Generic Field Checkbox

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

This topic contains 5 replies, has 2 voices.

Last updated by KentS9937 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1155529

I am trying to copy post data to a parent post if a generic field checkbox is checked. For some reason it's not working and I can't figure out why.

I based my solution partially on this article. https://toolset.com/forums/topic/update-parent-with-child-info/

I have two post types: HR Profile and Job Application. The post types are connected with a relationship hr-profile-job-app.
I have a form #5006 which is for editing a Job Application. This form has a Cred_generic_field copy-data-to-hr-profile which is a checkbox.

[cred_generic_field type='checkbox' field='copy-data-to-hr-profile']
{
"required":0,
"default":"",
"label":"Check to copy Job Application data to the HR Profile."
}
[/cred_generic_field]

This form also has a Cred field last-name

[cred_field field="last-name" force_type="field" class="form-control" output="bootstrap"]

I crafted the following code to copy the field value to the parent if the checkbox is checked. It's not working.

// COPY JOB APPLICATION FIELDS TO HR PROFILE
function job_app_data_action($post_id, $form_data) {
$update_trigger = $_POST ["copy-data-to-hr-profile"];
// IF ITS THE RIGHT FORM AND THE UPDATE CHECKBOX IS CHECKED
if (($form_data['id']==5006)&&($update_trigger==1)) {

$parent_hr_profile_id = toolset_get_related_post( $post_id, 'hr-profile-job-app', 'parent' );
$update_last_name = get_post_meta($post_id, 'wpcf-last-name', true);
update_post_meta($parent_hr_profile_id, 'wpcf-last-name', $update_last_name);
}
}
add_action('cred_save_data', 'job_app_data_action',10,2);

#1155536

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Kent

Not sure if something went awry with the chat, it said you weren't connected, so I've converted it to a thread to preserve your question. Let me read through it and I'll get back to you soon.

#1155569

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Kent

Looking through your code, nothing stands out as problematic.

I would try adding a bunch of error log messages to your code so that you can see what is happening at each step to confirm it is what is expected.

Something like this:

// COPY JOB APPLICATION FIELDS TO HR PROFILE
function job_app_data_action($post_id, $form_data) {
	$update_trigger = $_POST ["copy-data-to-hr-profile"];
	error_log('update_trigger: ' . $update_trigger);

	// IF ITS THE RIGHT FORM AND THE UPDATE CHECKBOX IS CHECKED
	if ( ($form_data['id']==5006) && ($update_trigger==1) ) {

		error_log("conditions matched");

		$parent_hr_profile_id = toolset_get_related_post( $post_id, 'hr-profile-job-app', 'parent' );

		error_log('parent_hr_profile_id: ' . $parent_hr_profile_id );

		$update_last_name = get_post_meta($post_id, 'wpcf-last-name', true);

		error_log('update_last_name: ' . $update_last_name );		

		update_post_meta($parent_hr_profile_id, 'wpcf-last-name', $update_last_name);

	}
}
add_action('cred_save_data', 'job_app_data_action',10,2);

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

That will create a debug.log file in your /wp-content/ directory which you can examine in any text editor. Try submitting the form and then inspect the log, which should hopefully help pin point what is going wrong...

#1155738

OK. I added the debug messages and turned on debugging as you suggested. If I'm reading the debug log correctly the $update_trigger is empty. Here's the contents of the debug log.

[29-Nov-2018 22:32:08 UTC] PHP Notice: Undefined index: text in /home/tableroc/public_html/hr.watertowernursing.com/wp-content/plugins/learndash-course-grid/learndash_course_grid.php on line 81
[29-Nov-2018 22:32:08 UTC] update_trigger:
[29-Nov-2018 22:32:10 UTC] PHP Notice: Undefined index: text in /home/tableroc/public_html/hr.watertowernursing.com/wp-content/plugins/learndash-course-grid/learndash_course_grid.php on line 81
[29-Nov-2018 22:32:18 UTC] PHP Notice: Undefined index: text in /home/tableroc/public_html/hr.watertowernursing.com/wp-content/plugins/learndash-course-grid/learndash_course_grid.php on line 81

By the way, when I saw the errors for learndash_course_grid.php I deactivated that plugin. It didn't change anything with my problem.

I did remove a space from the line setting the $update_trigger thinking maybe that was the problem. Didn't fix the problem. The code was changed from:
$update_trigger = $_POST ["copy-data-to-hr-profile"];
to
$update_trigger = $_POST["copy-data-to-hr-profile"];

Any thoughts on why $update_trigger isn't getting set? Have I defined my cred_generic_field correctly? I'm stumped.

#1156160

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I think you might find that the field isn't directly available as $_POST['field-slug'].

Add the following to dump the $_POST object itself to the error log, so you can see the format:

error_log('_POST: ' . print_r($_POST, true));

(I was going to test this myself now but we have a current issue with form submissions that we are urgently addressing, and should have a fix for later today.)

#1158996

My issue is resolved now. Thank you!