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

Comments

  1. Chad
    Chad December 4, 2024 at 1:52 am

    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

    Reply
    1. Roxy Stoltz
      Roxy Stoltz Staff December 5, 2024 at 3:16 am

      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. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.