Support

Account

Forum Replies Created

  • Hey feios, sure. Here’s an example of what I ended up doing. I’m sure there might be a more efficient but this worked well for me. Just update with your post type (if applicable) and the field key & name. In my case the field type was a Repeater, with an Image sub field (amongst others).

    
    function unattach_media($post_id) {
    
        // Only do this for my post type
        if ( get_post_type($post_id) == 'my_cpt' ){
    
            // vars
            $fields = false;
    
            // load from post
            if( isset($_POST['fields']) ) {
                $fields = $_POST['fields'];
            }
    
            global $wpdb;
    
            $field_key = 'field_key1234567890';
            $field_name = 'field_name';
            
            if (isset($fields[$field_key])) {
                $previous_images = get_field($field_name, $post_id);
                $new_images = $fields[$field_key];
    
                // create a lookup of ids for images in the post
                $new_lookup = array();
                if (!empty($new_images)) {
                    foreach($new_images as $row) {
                        array_push($new_lookup, $row['image']);
                    }
                }
    
                // create a lookup of images that were removed
                $removed_lookup = array();
                if (!empty($previous_images)) {
                    foreach($previous_images as $row) {
                        if (!in_array($row['image'], $new_lookup)) {
                            array_push($removed_lookup, $row['image']);
                        }
                    }
                }
    
                if (!empty($removed_lookup)) {
                    foreach($removed_lookup as $image) {
                        // unattach removed ones
                        $wpdb->update($wpdb->posts, array('post_parent'=>0), array('id'=>$image, 'post_type'=>'attachment'));
                    }
                }
    
                if (!empty($new_lookup)) {
                    foreach($new_lookup as $image) {
                        // attach added ones
                        $wpdb->update($wpdb->posts, array('post_parent'=>$post_id), array('id'=>$image, 'post_type'=>'attachment'));
                    }
                }
    
            }
    
        }
    
    }
    
    // run before ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'unattach_media', 1);
    
  • I’m getting this error also. Was working fine until latest PRO update on Sept 13.

    Also using svg files on current project. The quick fix works for jpg/img fine but obviously not for the svgs.

    Any word on an update with proper fix?

  • Any word on this one? It’s exactly what I need for an upcoming project. Relationship-style field for tags.

  • Got it. I’ll just use the acf/save_post action and detach any that have been removed. Thanks

  • Thanks Elliot, I’ll just switch to repeater for now.

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