Support

Account

Home Forums Front-end Issues Library: Uploaded to Post Reply To: Library: Uploaded to Post

  • After looking into this, the best I can come up with is to add an acf/save_post action.

    In this action you get a list of the images for the gallery

    
    // this will get unformatted value of gallery
    // an array of attachment post IDs
    // see https://www.advancedcustomfields.com/resources/get_field/
    $gallery = get_field('gallery_field_name', $post_id, false);
    

    Then loop over this list of ID, get the attachment post using get_post(), change the “post_parent” value and then use wp_update_post() to save the changes.

    Notes: An attachment can only go “attached” to a single post

    You must remove your action, inside of the action, before calling wp_update_post(). If you don’t then you create an infinite loop because acf/save_post is triggered by calling that function.

    
    add_action('acf/save_post', 'your_function_name');
    function your_function_name($post_id) {
      remove_filter(('acf/save_post', 'your_function_name');
    }