Skip Navigation

[Resolved] toolset_get_related_post get parent ID

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

Problem:
I am trying to get the parent post ID to take some post_meta from the parent and save it to the child post.

But i can't get the parent ID with my custom code:

https://toolset.com/forums/topic/toolset_get_related_post-get-parent-id/#post-1217040

Solution:

If the new "job" post is in non-published status, function toolset_get_related_post might not work.

But you can still get the parent "business" post ID with PHP $_POST variable, for example:

https://toolset.com/forums/topic/toolset_get_related_post-get-parent-id/#post-1217443

Relevant Documentation:

This support ticket is created 5 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by Benjamin 5 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#1217040

I am trying to get the parent post ID to take some post_meta from the parent and save it to the child post.
The parent post is set in CRED like:

<div class="hide">
[cred_field field="@business-job.parent" class="form-control" readonly='true' value="[user_post_data userid='' posttype='business' field='ID'] "]
</div>

That works just fine, but i can't get the parent ID with my custom code:

I already tried the lines below and a few more.

$job_id = $post_id;
$business_id = toolset_get_related_post( $job_id , array( 'business-job', 'business' ) );
$job_id = $post_id;
$business_id = toolset_get_related_post( $job_id , array( 'business-job', 'parent' ) );
$job_id = $post_id;
$business_id =  toolset_get_parent_post_by_type( $job_id, 'business' );

Thanks Ben

#1217369

Hello,

How do you setup your custom codes?
in most cases, you can use action hook "cred_save_data" to setup your custom PHP codes
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

If the relationship slug is "business-job", you can use it like this:

$job_id = $post_id;
$business_id = toolset_get_related_post( $job_id , 'business-job' );

More help:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
$relationship - string|string[] Slug of the relationship to query by or an array with the parent and the child post type.

#1217399

Hi,

thanks for your help, but it still returns 0.

I did my code like this:

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

function address_update($post_id,$form_data) {
	
	// ID 26 - CRED
	
    if ($form_data['id']==26) {

		$jobaddress = get_post_meta ( $post_id, "wpcf-adresse-choice", true );
		$job_id = $post_id;

		//Adresse vom Hauptstandort (Post --> Unternehmen)
		if ($jobaddress=="1"){
			//Get related Business ID
                       $business_id = toolset_get_related_post( $job_id , 'business-job' );
			
			$address_field = array('staat'=>'wpcf-staat','postleitzahl'=>'wpcf-postleitzahl','stadt'=>'wpcf-stadt','strasse'=>'wpcf-strasse','hausnummer'=>'wpcf-hausnummer');
			$address_field_region = array('region-at'=>'wpcf-bundesland-at','region-de'=>'wpcf-bundesland-de','region-ch'=>'wpcf-bundesland-ch');

			foreach ($address_field as $key => $field){
				$address_field[$key] = get_post_meta ( $business_id, $field, true );
			}

			foreach ($address_field_region as $key => $field){
				$address_field_region[] = get_post_meta( $business_id, $field, true );
			}

			//clear empty elements --> only one content
			$address_field_region = array_filter($address_field_region);
			//add region to array
			$address_field[region] = $address_field_region[0];
			// address in one line
			$address_one_line = implode(", ", $address_field);
			// add address to array
			$address_field[adresse] = $address_one_line;

			foreach ($address_field as $key => $field) {
				if ( ! add_post_meta( $job_id, 'wpcf-'.$key, $field, true ) ) {
					update_post_meta( $job_id, 'wpcf-'.$key, $field );
				}
			}

		}

			if ($jobaddress=="2"){

                       //Get related location ID
                      $location_id = toolset_get_related_post( $job_id , 'location-job' );


			$address_field = array('staat'=>'wpcf-staat','postleitzahl'=>'wpcf-postleitzahl','stadt'=>'wpcf-stadt','strasse'=>'wpcf-strasse','hausnummer'=>'wpcf-hausnummer');
			$address_field_region = array('region-at'=>'wpcf-bundesland-at','region-de'=>'wpcf-bundesland-de','region-ch'=>'wpcf-bundesland-ch');
	
			foreach ($address_field as $key => $field){
				$address_field[$key] = get_post_meta ( $location_id, $field, true );
			}
	
			foreach ($address_field_region as $key => $field){
				$address_field_region[] = get_post_meta( $location_id, $field, true );
			}
	
			//clear empty elements --> only one content
			$address_field_region = array_filter($address_field_region);
			//add region to array
			$address_field[region] = $address_field_region[0];
			// address in one line
			$address_one_line = implode(", ", $address_field);
			// add address to array
			$address_field[adresse] = $address_one_line;
	
			foreach ($address_field as $key => $field) {
				if ( ! add_post_meta( $post_id, 'wpcf-'.$key, $field ,true ) ) {
					update_post_meta( $post_id, 'wpcf-'.$key, $field );
				}

			}

		}
		
	}

}

I costumized it a little bit, but the code within the if-loops works, i tested it by setting the ID hardcoded.
Like you see the users can relate the job-post either to a business-post or to a location-post, with the result of updating the address of the job-post.

Thanks Ben

#1217443

How do you setup the Toolset post form? Can you take a screenshot for it.
If the new "job" post is in non-published status, function toolset_get_related_post might not work.

But you can still get the parent "business" post ID with PHP $_POST variable, for example:

$business_id = $_POST['@business-job_parent'];

More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1217540

My issue is resolved now. Thank you!

Your hint with the post status was gold. Since the post is posted with status "draft" the hook didn't work.
But with your provided code it works fine!

$business_id = $_POST['@business-job_parent'];