Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having a trouble un-attaching media from a post without deleting it entirely from my site. Is there a simple way to unattach an image from a post for instance?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

I learned from Brian Fegter that you can Un-attach an image from a post while participating in this question: Can I attach an image to a different post?

It isn't exactly "simple", but not to difficult. It's the only solution I've been able to find.

share|improve this answer
add comment

Is this what you're looking for?

e.g. Remove all image attachments from a post

//get all image attachments
$attachments = get_children( 
                    array(
                        'post_parent'       => $post->ID,
                        'post_mime_type'    => 'image',
                        'post_type'         => 'attachment'
                    ) 
                );

//loop through the array
if( !empty( $attachments ) ){   
    foreach( $attachments as $attachment ){
        // Update the post into the database
          wp_update_post( array(
                    'ID' => $attachment->ID,
                    'post_parent' => 0
                )
            );
    }
}

However, please take note of the caution when using wp_update_post.

Alternate method using $wpdb

//replace this with the above inside the foreach block;
global $wpdb;
$wpdb->query(
    "
    UPDATE $wpdb->posts 
    SET post_parent = 0
    WHERE ID = $attachment->id
        AND post_type = 'attachment'
    "
);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.