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


Attach images created in front end form to custom post

  • Hi,
    I am having a challenge attaching images from the image uploader field to the custom post created using the front end form.

    In my wordpress site, i have the form loaded in a page template i created e.g “add-post.php”

    When i save my Post, i find out that the images are attached to the page template and not the post. Is there any way to attach the images to the post rather than the page id?

    Thank you.

  • Hi @realnsleo

    Thanks for the question. The WP uploader uses a JS variable stored in acf.o.post_id to set the ‘attached to’ data of an image.

    I’m not sure you would be able to edit this to the new post_id as the new post_id has not yet been created…

    I will soon be releasing a new field for ACF which is a basic file input field and will allow you to bypass this issue completely.

    Until then, I’m not sure what the solution is…

  • Hi Elliot,
    First off i’d like to thank you for providing this wonderful plugin. It has done wonders for me so far.

    I found a dirty hack of using the ‘pre-save-post’ filter function to get the attachment ID and programmatically change the post parent. The function is:

    function attachment_change_parent($aid,$pid) {
    	global $wpdb;
    	$query = "UPDATE {$wpdb->prefix}posts
    		SET post_parent = $pid
    		WHERE ID = $aid";
    	return ($wpdb->query( $wpdb->prepare($query) )) ? true : false;

    It works so far but it is not the best option. I will eagerly await your update on this.

    Otherwise thanks again. Keep up the good hard work sir!

  • Hi @realnsleo,

    I’m having the same problem.
    Could you show me the entire solution for this with the implementation of the pre-save-post filter?

    Much appreciated! Thanks.

  • Hi @realnsleo,

    I’ve managed to create a working solution. Thanks for pointing me in the right direction!

    And Elliot, thank you so much creating this plugin.

  • Hi @realnsleo can you please elaborate on your solution? Do you just add this and do nothing else?

    When I just add this directly into my functions.php file it does nothing. Am I supposed to insert this function inside of my “my_pre_save_post” function? Please advise.

  • Hi @tsmulugeta
    Wow, I do not remember when I last used this function as I resorted to using the repeater fields extension. However, If my memory serves me right, my goal was to create a new post of a custom type and attach an image to it, and not the page that the front end form was generated on. To save the post, this is the code I “think” i placed in my functions.php file to save the custom post. If it helps, I was using this code for a simple online classifieds website to post ads:

    function my_pre_save_post( $post_id )
        // check if this is to be a new post
        if( $post_id != 'new' )
            return $post_id
        // Create a new post
        $ad = array(
            'post_status'    => 'published',
            'post_content'   => $_POST['description'],
            'post_title'     => $_POST['title'],
            'post_type'      => 'ads',
            'post_author'    => get_current_user_id(),
            'comment_status' => 'closed',   // if you prefer
            'ping_status'    => 'closed'    // if you prefer
        // insert the post
        $post_id = wp_insert_post( $ad ); 
        // set post (ad) terms
        $category = intval( $data['category'] );
        wp_set_object_terms( $post_id, array( $category ), 'ad-categories' );
        wp_set_object_terms( $post_id, explode( ',', $data['tags'] ), 'ad-tags' );
        // now this is where I retrieved the submitted image id
        // from the form and attached it to the post
        attachment_change_parent( $_POST['ad_image'], $post_id );    
        // return the new ID
        return $post_id;
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
    // this function changes the parent of the attached image to the post
    function attachment_change_parent($aid,$pid) {
    	global $wpdb;
    	$query = "UPDATE {$wpdb->prefix}posts
    		SET post_parent = $pid
    		WHERE ID = $aid";
    	return ($wpdb->query( $wpdb->prepare($query) )) ? true : false;

    Basically, from the front end form, as soon as the image is uploaded (via javascript), it’s attached to the page on which the form resides, but what I did was during the “pre_save_post” hook, I get the submitted image id and change the id of the page it’s attached to to the post as soon as the post is created.

    I hope this makes some sense and I sure hope it points you into finding a solution. I am here in case you need any more explanation with anything.

    Thank you!

  • Thanks @realnsleo I tried the code above you posted but it didn’t work for me 🙁

  • @tsmulugeta would you mind telling me exactly what you are trying to do/achieve? Maybe I would be of more help then

  • 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

    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!

  • That looks much better! But what about situation where user does not upload media file, but chooses from already uploaded posts instead? I think if it’s done via WordPress, then attachment record is cloned and then attached to new post_parent. Then your solution would break previous ties of existing attachment, wouldn’t it?

  • Nevermind, gallery shows up only unattached images, so that’s not a problem at all! Thanks @markbloomfield!

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

The topic ‘Attach images created in front end form to custom post’ is closed to new replies.