Auto-attach Images from Child Form to Parent Post

This snippet assumes you have a parent form that is used to generate to a new post via the Advanced Post Creation add-on – and – a child form that contains a Post Image field. With this snippet in place, all images uploaded via the child form will be automatically attached to the post generated for the parent form. Posts generated by the child form will be deleted.

Code

Filename: gpnf-attach-child-form-post-images-to-parent-post.php

<?php
/**
 * Gravity Perks // Nested Forms // Auto-attach Images from Child Form to Parent Post
 * https://gravitywiz.com/documentation/gravity-forms-nested-forms/
 *
 * This snippet assumes you have a parent form that is used to generate to a new post via the Advanced Post Creation
 * add-on – and – a child form that contains a Post Image field. With this snippet in place, all images uploaded via
 * the child form will be automatically attached to the post generated for the parent form. Posts generated by the
 * child form will be deleted.
 */
// Update "123" to your parent form ID.
add_action( 'gform_advancedpostcreation_post_after_creation_123', function( $post_id, $feed, $entry, $form ) {

	$parent_entry  = new GPNF_Entry( $entry );
	$child_entries = $parent_entry->get_child_entries();

	foreach ( $child_entries as $child_entry ) {

		if ( ! $child_entry['post_id'] ) {
			return;
		}

		$attachments = get_posts( array(
			'post_type'      => 'attachment',
			'posts_per_page' => -1,
			'post_parent'    => $child_entry['post_id'],
		) );

		foreach ( $attachments as $attachment ) {
			wp_update_post( array(
				'ID'          => $attachment->ID,
				'post_parent' => $post_id,
			) );
		}

		wp_delete_post( $child_entry['post_id'] );

	}

}, 10, 5 );

Comments

  1. Chad Nilsson
    Chad Nilsson November 17, 2024 at 7:06 am

    Is there a snippet will do this the other way and attached files uploaded to the parent form, to the child form notifications?

    Use case:

    I have a form where I submit audition requests to my users. I use a nested form to send this I formation out to multiple users at once, and it works great, but when I want to attach pdfs of the script or other info….i can’t, so it kind of renders the setup a bit useless, and I have to stick with a parent notification that just bccs in those emails, so they all get the attachment.

    Reply
    1. Scott Ryer
      Scott Ryer Staff November 20, 2024 at 1:31 pm

      Hi Chad,

      We have followed up via email. This isn’t currently supported, but we’re interested in adding support if you have a Pro license.

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.