Skip Navigation

[Resolved] Update grandparent with child values

This support ticket is created 5 years, 5 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
- 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 2 replies, has 2 voices.

Last updated by MattI4840 5 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#1122821

Tell us what you are trying to do?

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 );

Thanks in advance!

#1123541

Nigel
Supporter

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

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

Hi Matt

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.

[09-Oct-2018 09:34:08 UTC] _POST: Array
(
    [@project-task_parent] => 43
    [form_submit_1] => Submit
    [_cred_cred_wpnonce_cred_form_81] => 95002792b5
    [_cred_cred_prefix_post_id] => 285
    [_cred_cred_prefix_cred_container_id] => 43
    [_cred_cred_prefix_form_id] => 81
    [_cred_cred_prefix_form_count] => 1
)

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:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

You would use that something like this:

// we already have a $parent_id
$grandparent_id = toolset_get_related_post( $parent_id, 'relationship-slug' );

You just need to edit the relationship-slug (for the grandparent << parent relationship).

#1124068

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.