I have two one to many relationships set up like this: Warehouse->InboundShipments->Containers
When a container is confirmed to be delivered (the cred-update form is submitted), I need to update several fields in the grandparent (aka warehouse) post.
Below is the code I've tested with but the fields are not updating as expected. Is there a way to reference the grandparent directly? Or is the two step process to reference them needed, and if so where am I screwing up the syntax?
//get the container's parent aka the inbound shipment
$inbound_shipment = $_POST['@transaction-container_parent'];
//get the inbound shipment's parent aka the warehouse
$warehouse = $inbound_shipment['@warehouse-transaction_parent'];
//get the value stored in the warehouse's dt98-totes field
$warehouse_totes = get_post_meta( $warehouse, 'wpcf-warehouse-dt98-totes', true);
//add the container totes value, the warehouse totes value, and update the warehouse totes value with the total of the two.
$total_totes = $warehouse_totes + $container_totes;
update_post_meta( $warehouse, 'wpcf-warehouse-dt98-totes', $total_totes );
If you have looked at the $_POST object (it seems you have) when submitting a form to publish a grandchild post, you should have noticed that it contains the id of the parent post (set by the parent selector field in the form), but doesn't have any reference to the grandparent.
Here's an example of the $_POST object for a form on my site to publish grandchild task posts which has relationships of organization << projects << tasks.
So, if I wanted to set a field on a grandparent post, I would need to take the id of the parent and then use the relationships API to get the id of the grandparent (the parent of the parent).
In this case, where there is only one parent of the parent, you could use toolset_get_related_post: