Support

Account

Home Forums Front-end Issues Image not attached to post on save acf form

Solved

Image not attached to post on save acf form

  • I have a front end acf form that creates a post. The form includes a gallery type field.

    On save post, the images chosen using the acf form gallery field are saved to Media Library, but they are not attached to the post that was just created.
    On update form/post, the images added are being saved to media library and they are attached to the post.

    Because the images are not attached to the post, they don’t show up in the list of images uploaded to post for the user who created the post.

    What do I need to do so the images are attached to the post when the post is created(on save post)?

    /* register ACF form in function.php*/
    acf_register_form(array(
    ‘id’ => ‘new-guitar’,
    ‘post_id’ => ‘new_post’,
    ‘post_title’ => true,
    ‘new_post’ => array(
    ‘post_type’ => ‘guitars’,
    ‘post_status’ => ‘publish’
    ),
    ‘return’ => home_url(),
    ‘submit_value’ => ‘Save guitar’,

    ));

    //code in add new post template (image not attached to post)
    acf_form(‘new-guitar’);

    //code in update post template – works(image attached to post)
    acf_form(array(
    ‘post_title’ => true,
    ‘submit_value’ => ‘Update guitar!’
    ));

    Thanks!

  • You need to write a filter so that when the post is saved the featured image is set for the post. Grab the image info from the acf field and create a filter for ‘acf/save_post’ action. The function should have something like this:
    add_post_meta($post_id, ‘_thumbnail_id’, $value);

  • I tried:
    update_post_meta($post_id, ‘_thumbnail_id’, $image_id);

    It sets my image as featured image, and it displays correctly, but the image is still unattached to the post, therefor not in the media uploaded to post list.

    I found this function, wp_media_attach_action(). Not sure how to use it though.

  • What you need to do is to update the “attachment” post and set “post_parent” to the post you want to attach it to.

    
    $attachment = array(
      'ID' => $attachment_id, // image ID
      'post_parent' => $post_id, // the post you want it attached to
    );
    wp_update_post($attachment );
    
  • John Huebner’s reply and this post sent me into the right direction:
    https://support.advancedcustomfields.com/forums/topic/acf_form-attachment-to-media-gallery/

    Modified it for gallery field.

    
    function my_save_post( $post_id ) {
            //attachment to media gallery 
    	$my_files = get_field('my_gallery', $post_id);	
    
    	if($my_files){
    
    		foreach( $my_files as $my_file ):
    			$file_id = $my_file['ID'];
    			$post_to_update = array(
    				'ID'           => $file_id,
    				'post_parent'  => $post_id
    			);
    			wp_update_post( $post_to_update );
    		endforeach;
    	}
    }
    add_action('acf/save_post', 'my_save_post', 15);
    
  • image slider not uploaded
    how about this?
    im tired

  • That code works well for items being added, but doesn’t cater for when they are removed.

    Is there a before and after version of the gallery array available, which I could use to see what was removed and then set the post_parent for those IDs to 0?

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Image not attached to post on save acf form’ is closed to new replies.