Image still linked as attachment to page even though it has been deleted

I have been working on an image gallery feature where I grab images which are attached to pages and display them in various places via the get_posts() function. However, I am finding that when I remove images from the page that they have been placed that they still seem to be set as attachments. I’m just wondering if there’s something I’m mis-understanding about attachments or if this is buggy behaviour?

It may be worth me adding that I am getting images as attachments of pages because I need to be able to categorise the images and display them via categories in different places on the site. If I was able to categorise images directly I realise I wouldn’t need to worry about this problem I’m encountering!

Related posts

Leave a Reply

3 comments

  1. Deleting an image from a post or page is a confusing process. When you upload an image to a post/page, it’s added to that post/page’s gallery. You can then insert the image into the content of the post/page and edit content as you see fit. When you click on the image in the wysiwyg editor, you’ll see two icons – one to edit the image’s settings and one to remove it from the editor – this second icon does not delete the image even though it looks like a typical “delete” icon.

    When you press that “delete” button, it removes the image from the visual editor but keeps it in the post/page’s gallery.

    To actually remove/delete the image, you need to do the following

    • Click the “Add Image” button to view the image screen
    • Click the “Gallery” tab to view what images are currently attached to the post
    • Click the “Show” link for the image you want removed.
    • Click “Delete” towards the bottom of the image detail screen

    You’ll be asked to confirm deletion, then the image will be unattached to the post/page and removed entirely from WordPress.

  2. I’ve never played with this, but you can attach taxonomies to attachments (either built in or custom) like this:

    register_taxonomy_for_object_type( 'category', 'attachment' );
    

    Hook that onto 'init' to be able to categorize attachments according to category.

    I don’t know how you’re ‘removing’ the image, but if you’re deleting it from WordPress, it shouldn’t be set as an attachment for that page anymore. The other way to unset the image as an attachment is by going into the database and removing the page id from the attachment’s 'post_parent' column.

    You might also look into the File Gallery plugin. It does a lot more than you might be interested in, but it lets you unattach images from posts.

  3. Just a vague guess, but might you be getting attachments from all revisions / statuses of pages? If you limit the get_posts() call to only published posts/pages, you might see the image in question disappear.