Support

Account

Home Forums Front-end Issues Attach images created in front end form to custom post Reply To: Attach images created in front end form to custom post

  • This is quite an old post, but I had to do something similar here and I did it in a slightly different way to the solution above so figured I’d share:

    In my scenario, the user uploads images (which are correctly assigned a post_parent), and a couple PDF posts (which weren’t being correctly assigned).

    
    function attachment_change_parent($aid,$pid) {
      $update_attachment_post = array(
        'ID'            => $aid,
        'post_parent'   => $pid
      );
    
      wp_update_post($update_attachment_post);
    }

    Instead of doing a SQL query, I just used the native wp_update_post function to update the attachment’s post_parent to be the ID of the newly created post.

    Then my PDF field was set to return an array, not a post ID. So in my pre_save_post function, I had to get that ID slightly differently:

    attachment_change_parent($_POST['acf']['field_58a18e2988392'][0], $post_id);

    …where you’d obviously just update that field key to be the key of your upload field.

    Works a dream. Thanks!