Attach Files uploaded via GPML to Posts created with GFAPC + ACF

Attach Files uploaded via GPML to Posts created with GFAPC + ACF

Instructions

Code

Filename: gpml-apc-attach-files-to-post.php

<?php
/**
 * Gravity Wiz // Gravity Forms // Attach Files uploaded via GPML to Posts created with GFAPC + ACF
 *
 * Attach Files uploaded via GPML to Posts created with GFAPC + ACF
 *
 * Instructions:
 *  1. Install per https://gravitywiz.com/how-do-i-install-a-snippet/
 */
add_action( 'gform_advancedpostcreation_post_after_creation', function( $post_id, $feed, $entry, $form ) {
	foreach ( $feed['meta']['postMetaFields'] as $post_meta_field ) {
		$field_id         = rgar( $post_meta_field, 'value' );
		$field            = GFAPI::get_field( $form, $field_id );
		$is_media_library = rgar( $field, 'uploadMediaLibrary' );

		if ( $is_media_library ) {
			$file_ids = gp_media_library()->get_file_ids( $entry['id'], $field->id );
			foreach ( $file_ids as $file_id ) {
				wp_update_post( array(
					'ID'          => $file_id,
					'post_parent' => $post_id,
				) );
			}
		}
	}
}, 10, 4 );

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.