Skip Navigation

[Resolved] Get parent post id and meta field is not working in cred_save_data

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

Problem: I have a Form that is used to create child post in a one-to-many post relationship. I would like to get the value of a custom field from the related parent post in a cred_save_data callback, but it does not seem to be working.

Solution: The relationship is not yet set in a cred_save_data hook, so you can either use the cred_submit_complete hook instead, or access the parent post ID directly from the parent select field in the $_POST superglobal.

$parent_post_id = $_POST['@relationshipslug_parent'];
$parent_field_value = get_post_meta( $parent_post_id, 'wpcf-fieldslug', true);

100% of people find this useful.

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

Author
Posts
#1921667

I have a toolset form (id=1138) to add a CPT 'phv'.
In this form I first select a parent posttype 'persoon' based on a relationship that i called 'relatie_personen_phvs' (1 'persoon' can have multiple 'phv') and then I can enter the rest of the fields.
I need the field 'persoon_geboortedatum' (date of birth) stored in the parent post 'persoon' to calculate something by using PHP.

My problem is that I get 0 returned as the related post id, so my code below is not working correctly but I am unable to find the issue:

For debugging purposes: I am trying to get the post id of the related parent post in a field aphv.

function calculate_fields( $post_id ) {

if ( get_post_type( $post_id ) == 'phv' ) {

$aphv_male = 0;
$aphv_female = 0;

// Bereken aantal jaren van geboortedatum
// eerst de post id ophalen van de persoon
$related_post = toolset_get_related_post( $post_id, 'relatie_personen_phvs', 'parent' );
//$related_post = toolset_get_related_post( $post_ID, array('phv', 'persoon') );

// $dateOfBirth = date ("Y-m-d",(get_post_meta($related_post, 'wpcf-persoon_geboortedatum', true)));
$dateOfBirth = get_post_meta($related_post, 'wpcf-persoon_geboortedatum', true);
$today = date ("Y-m-d");
// $c = date_diff(date_create($dateOfBirth), date_create($today));

$datetime1 = date_create($dateOfBirth);
$datetime2 = date_create($today);
$diff = date_diff($datetime1, $datetime2);
$c = floor($diff->format("%y"));

// Metingen
$a = get_post_meta($post_id, 'wpcf-phv_lengte', true);
$b = get_post_meta($post_id, 'wpcf-phv_zithoogte', true);
$d = get_post_meta($post_id, 'wpcf-phv_gewicht', true);

// Bereken APHV voor jongens
// $aphv_male = '-9.236' + ('0.0002708'*(($a-$b)*$b)) + ('-0.001663'*($c*($a-$b))) + ('0.007216'*($c*$b)) + ('0.02292'*(($d/$a)*'100'));
// $aphv_male = $a + $b + floor($c) + $d;
$aphv_male = $related_post;

// $aphv_female = '-9.376' + ('0.0001882'*(($a-$b)*$b)) + ('0.0022'*($c*($a-$b))) + ('0.005841'*($c*$b)) - ('0.002658'*($c*$d)) + ('0.07693'*(($d/$a)*'100'));

update_post_meta( $post_id, 'wpcf-phv_aphv', $aphv_male );

}
}
add_action( 'save_post', 'calculate_fields', 99 );

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
// if a specific form
if ($form_data['id']==1138) {
calculate_fields( $post_id );
}
}

#1921803
$related_post = toolset_get_related_post( $post_id, 'relatie_personen_phvs', 'parent' );

Hello, in the current software toolset_get_related_post is not ready in a cred_save_data callback when the parent post is selected in a child post Form. There is a timing issue, where the relationship is not yet saved when the cred_save_data hook is fired. One workaround is to use the cred_submit_complete hook instead, since it is fired later in the sequence. The toolset_get_related_post API should return the correct related post during the cred_submit_complete callback.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete

Another workaround if you must use the cred_save_data hook, instead of cred_submit_complete, is to access the selected parent post directly in the $_POST superglobal, usually like this:

$_POST['@relationshipslug_parent']

You would replace relationshipslug with the slug of this O2M relationship.

#1921949

I would prefer to use the $_POST option. Can you give an example to get a metafield from the parent?

Parent CPT = 'persoon'
Metafield = 'persoon_geboortedatum' (contains a date of birth)
Relationship slug is 'relatie_personen_phvs'

thanks

#1921977

You should be able to get a Types custom field value from the related post like this in a cred_save_data hook:

$persoon_id = isset($_POST['@relatie_personen_phvs_parent']) ? $_POST['@relatie_personen_phvs_parent'] : 0;
$geboortedatum = get_post_meta($persoon_id, 'wpcf-persoon_geboortedatum', true);
#1922197

That worked! Thanks!

#1922213

Glad to help. Feel free to create tickets as needed if you run into other issues.

#1923319

One thing although: the post keeps saving with Auto Cred Save. How to create a title based on a custom field?

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/create-post-title-automatically-based-on-custom-field-value/

#1926743

I'll open a new ticket so we can discuss this issue in more detail.