Skip Navigation

[Resolved] Cannot Get Parent Post ID on Cred_Save_Data with New Relationships API

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.

Our next available supporter will start replying to tickets in about 2.73 hours from now. Thank you for your understanding.

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)

Author
Posts
#1121627

Before the new relationships functionality, I was able to create an intermediary post that linked two parent posts of different types (the former way of doing many-to-many). When creating the intermediary post, I retrieved the post id for both parents on cred_save_data and used these to auto generate content.

However, after updating to the new relationships API I am no longer able to access the parent post ids on cred_save_data.

The new form field is as follows...

[cred_field field='@events_appearances.parent' select_text='--- not set ---' class='form-control' output='bootstrap']

I have tried all of the following solutions to try to retrieve the parent post ID but none of them have worked...

// Get ID of Parent Event Selected in CRED Form
        //   $event_id = $_POST['_wpcf_belongs_events_id'];
	   $event_id = $_POST['@events_appearances.parent'];
	//   $event_id = toolset_get_related_post( $post_id, 'events_appearances' );
	//   $event_id = get_post_meta($post_id, '_wpcf_belongs_events_id', true);
 	//   $event_id = toolset_get_parent_post_by_type( $post_id, 'event' );
	   error_log(print_r($event_id, true));

Note: the parent CPTS are 'Vessels' and 'Events', both of which have a one-to-many relationship with the Intermediary CPT 'Appearances'.

I do not want to use the new many to many relationships format for this particular relationship at the moment, I simply want to retrieve the parent post IDs on cred_save_data so that I can access them in php.

#1121934

In a Post Form that manipulates the Child (Many Part) of a modern Relationship, you should be able to access the set (and saved) parent Posts in a cred_submit_complete() (or later) from the very Child Post manipulated, using the new relationships API:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
In a one to many like you have, where you get the one (parent) post from a many(child) ends, you would use toolset_get_related_post().
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

$related_post_id = toolset_get_related_post($post_id, $relationship );

Like this in a full example:

add_action('cred_submit_complete', 'ts_update_post_field_with_parent_post_id',10,2);

function ts_update_post_field_with_parent_post_id($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==39)
    {
    	$relationship = 'page-post';//Edit the exact slug of the relationship you want to target
        $related_post_id = toolset_get_related_post($post_id, $relationship );//can be repeated to get another related post of other type

        //Example of what to do with such data
        update_post_meta($post_id, 'wpcf-numeric-single-value', $related_post_id);

//To debug:        
//var_dump($whatever);die();
    }
}
#1121943

Hi Beda,

I have tried the solution you've given but it doesn't appear to be working.

I include my full code below for reference, can you see any obvious mistakes in how I'm getting the parent post IDs?

Debug.log...

[06-Oct-2018 08:14:02 UTC] Starts...
[06-Oct-2018 08:14:02 UTC] 0
[06-Oct-2018 08:14:02 UTC] 0

Var_dump Debug...

string(19) "vessels_appearances" int(0) string(18) "events_appearances" int(0)

Full Code...

// New Appearance Form - Save Vessel and Event Details to Appearance Post
   add_action('cred_submit_complete', 'save_event_and_vessel_details_to_appearance',10,2);
   function save_event_and_vessel_details_to_appearance($post_id, $form_data)
   {
    // if a specific form
       if ($form_data['id']==95) {

	error_log("Starts...");

	// Get ID of Parent Event
	   $eventrelationship = 'events_appearances';
           $event_id = toolset_get_related_post($post_id, $eventrelationship );
	// Debug   

	   error_log(print_r($event_id, true));

	// Get ID of Parent Vessel
	   $vesselrelationship = 'vessels_appearances';
           $vessel_id = toolset_get_related_post($post_id, $vesselrelationship );
	// Debug   
	   error_log(print_r($vessel_id, true));

 	// Get Vessel and Event Details
	   $vessel_name = get_the_title( $vessel_id );
	   $vessel_image_id = get_post_thumbnail_id( $vessel_id );
	   $event_name = get_the_title( $event_id );
           $eventdateto = get_post_meta( $event_id, 'wpcf-event-date-to' ,true);

        // Save Parent Vessel and Event Field values to Appearance Post
           update_post_meta( $post_id, 'wpcf-appearance-expiry-date', $eventdateto );
	   update_post_meta( $post_id, 'wpcf-appearance-vessel-name', $vessel_name );
  	   update_post_meta( $post_id, 'wpcf-appearance-event-name', $event_name );
	   set_post_thumbnail( $post_id, $vessel_image_id );

	// Get Author Details
	   $current_user = wp_get_current_user();
	   $author = $current_user->nickname;

	// If Port Visit
	   if( has_term( 'port-visit', 'event-types', $event_id ) ) {

		   $eventcountry_list = wp_get_post_terms($event_id, 'countries', array("fields" => "all"));
		   $eventcountry = $eventcountry_list[0]->name;
		   $eventyear = types_render_field( "event-date-from", array( "id" => "$event_id", "format" => "Y" ) );
		   $eventlocation = types_render_field( "event-location", array( "id" => "$event_id", "output" => "raw" ) );
		   $secondarytitle = $vessel_name . " is coming to " . $eventlocation . ", " . $eventcountry . " in " . $eventyear;		   
		   $introline = $author . " has announced that " . $vessel_name . " will be open to the public when she visits " . $eventlocation . " in " . $eventyear . ".";

		// Populate Appearance Post Fields
	   	   update_post_meta( $post_id, 'wpcf-secondary-title', $secondarytitle );
		   update_post_meta( $post_id, 'wpcf-intro-line', $introline ); 

	   } else {

		   $secondarytitle = $vessel_name . " will take part in " . $event_name;
		   $introline = $author . " has announced that " . $vessel_name . " will be taking part in " . $event_name . ".";

		// Populate Appearance Post Fields
	   	   update_post_meta( $post_id, 'wpcf-secondary-title', $secondarytitle );
		   update_post_meta( $post_id, 'wpcf-intro-line', $introline ); 

	   }

	// DEBUG
	   var_dump($vesselrelationship, $vessel_id, $eventrelationship, $event_id);die();

     }
}
#1121949

Check what $post_id (of the Form hook) gives you.
It should give you the Post ID of the Post you are manipulating with this Form, and hence, the CHILD post.
Then make sure to copy the releationships slug, and that must be a modern, not migrated, not old one to many relationship.
That slug goes to the variable deteremining the relationship.

Then, if that is correct, the code $related_post_id = toolset_get_related_post($post_id, $relationship );//can be repeated to get another related post of other type and it returns the ID of the connected post, unless there is no connected post.

I tested that locally, it may be on your install other code or plugins or a theme interferes, can you test this on a clean test copy?
It seems to me you are not receiveing the proper post id to start with.

#1121957

Hi Beda,

Thanks for the prompt response, the post id comes through correctly though I now realise there is a difference between 'Migrated' and 'Modern'. The relationships in question are currently of the Migrated type so I will look into replacing these first then get back to you if the issue persists.

#1122536

Hello,

Please update this thread if you still more assistance.

#1145738

Hi there,

I am having the exact same problem. Parent Post Type "Site -> Child Post Type "Truck"

---
function save_siteId( $post_ID ) {

if ( get_post_type( $post_ID ) == 'truck' && isset( $GLOBALS['post'] ) ) {

// Get Site from RelatedItem

$truck_id = get_post( get_the_ID() ); // Get ID of current truck

$site_id = toolset_get_related_post( $truck_id, 'site-truck', 'parent' );

// return $site_id;

update_post_meta( $post_ID, 'wpcf-trucksite', $site_id );
}
}
add_action( 'save_post', 'save_siteId', 20, 2 );

----
This works, but only if the post is updated. For a new post, this does not work, it only writes "0" in my field "wpcf-trucksite".