Attach Child Entry to Parent Programmatically
Code
Filename: gpnf-attach-child-entry-programatically.php
<?php
/**
* Gravity Perks // Nested Forms // Attach Child Entry to Parent Programmatically
* https://gravitywiz.com/documentation/gravity-forms-nested-forms/
*/
// Update "124" to your child form ID.
add_filter( 'gform_entry_post_save_124', function( $entry ) {
$entry = array_replace( array(
// The ID of the parent entry.
GPNF_Entry::ENTRY_PARENT_KEY => 234,
// The ID of the parent form.
GPNF_Entry::ENTRY_PARENT_FORM_KEY => 123,
// The ID of the Nested Form field on the parent form.
GPNF_Entry::ENTRY_NESTED_FORM_FIELD_KEY => 4,
), $entry );
GFAPI::update_entry( $entry );
return $entry;
} );
This one has been awesome for me – thank you!
Also, in case it’s helpful to anyone else, I expanded this for my use case to bulk attach child entries to parent entries, based on a matching field key between the two. Essentially it loops through the child form entries, checks the key field, loops through the parent field entries, and matches the key field, and attaches any matching child entries to the parent entries.
I used it to attach about 450 child entries to about 400 parent entries, where some parent entries had multiple matching child entries. Worked great. you’d just hav eto udate the form ids and field ids.
@Gravitywiz – feel free to take this snippet and improve it/make it more clear, or do whatever if it might be helpful for someone else. Or just ignore it up to you haha.
https://github.com/nilssondigital/Code/blob/main/Bulk%20attach%20child%20entries%20to%20parent%20entries
Hey Chad,
It’s great to hear you’ve been finding the snippet helpful!
That is awesome – thanks for sharing that! I’m sure this will be really helpful for others with a similar use-case. 🙂