Home › Forums › General Issues › Unattached image still shows as attached
I’m using the Repeater with Image field rows and after saving the post the Media library (and also any WP_Query’s for attachments) correctly show the image as being attached to the post. If I then edit the post and delete an image/row and save the post, the Media library (and again any attachments query) still shows the image as being attached to the post.
Is this a bug, either with WordPress or ACF, or something that can be resolved?
I’m relying mostly on the WP_Query to correctly not include those that aren’t actually attached any more. Currently even though it’s technically removed from the post, the image still gets included since it appears to still be attached.
Same thing happens with images via the repeater and also the Gallery field.
Any ideas / help / pointers much appreciated.
Hi @runpixelrun
This isn’t so much of a bug, but more of a problematic feature of WP and ACF.
When an image is uploaded, WP will attach it to the post which is being edited. This is outside of ACF.
When you remove the image from the field, ACF is only modifying the postmeta, not the actual image, so the connection to the post remains.
To remove this connection is potentially harmful to some websites which rely on the default WP behavior.
For now, your best bet is to hook into the acf/update_value, or even the acf/save_post actions and modify the image connection manually.
Good luck
Thanks
E
Got it. I’ll just use the acf/save_post action and detach any that have been removed. Thanks
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);
Thank you for that! Worked perfectly!
Any idea on how to sort the attachments in same order as the repeater???
Apologies for replying to a really old post, but I have the same issue when media is added/removed from the ACF Gallery and the above code no longer works.
Can anyone point me in the direction of what I need to update?
I managed to work it out, so am posting it here in case anyone else comes looking for it 🙂
Points to note:
– Thinking that it may help someone else, I’ve left a few debugging lines in the below code
– I am also setting the first entry in the gallery array as the Featured Image / Thumbnail because it isn’t an option I am making available to the users of my site.
add_action('acf/save_post', 'manage_portfolio_media', 1);
function manage_portfolio_media($post_id) {
if ( get_post_type($post_id) == 'ocean_portfolio' ){
$acf = false;
if( isset($_POST['acf']) ) {
$acf = $_POST['acf'];
}
$field_key = 'field_5e92f3aacc8a6';
$field_name = 'attachments';
if (isset($acf[$field_key])) {
$previous_images = get_field($field_name, $post_id);
$new_images = $acf[$field_key];
// create a lookup of ids for images in the post
$new_lookup = array();
if (!empty($new_images)) {
foreach($new_images as $imageid) {
array_push($new_lookup, $imageid);
}
}
// create a lookup of images that were removed
$removed_lookup = array();
if (!empty($previous_images)) {
foreach($previous_images as $image) {
error_log('Previous Array = ' . $image['ID']);
if (!in_array($image['ID'], $new_lookup)) {
array_push($removed_lookup, $image['ID']);
}
}
}
if (!empty($removed_lookup)) {
foreach($removed_lookup as $imageid) {
// unattach removed ones
error_log('Removed Item = ' . $imageid);
wp_update_post( array( 'ID' => $imageid, 'post_parent' => 0 ) );
}
}
if (!empty($new_lookup)) {
set_post_thumbnail( $post_id, $new_lookup['0'] );
error_log('New Thumbnail = ' . $new_lookup['0']);
foreach($new_lookup as $imageid) {
// attach added ones
error_log('New Array = ' . $imageid);
wp_update_post( array( 'ID' => $imageid, 'post_parent' => $post_id ) );
}
}
}
}
}
The topic ‘Unattached image still shows as attached’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.