2

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!

6
  • An example of your get_posts() call would be REALLY helpful here
    – Bobby Jack
    Commented Aug 18, 2010 at 13:50
  • I didn't include an example of the get_posts() call as the issue I seem to be seeing is more to do with WordPress' handling of attaching / removing images as attachments. It's not so much get_posts() behaving incorrectly but with image handling inside the Admin. Commented Aug 18, 2010 at 14:11
  • How are you "removing images from the page that they have been placed?" If you're just deleting the image from the content of the post/page, that won't remove it.
    – EAMann
    Commented Aug 18, 2010 at 15:18
  • @EAMann: Does that mean there is no means to remove an image as an attachment of a page once it has been inserted and deleted? Seems strange behaviour if so. I know I can edit the database to remove the reference to post_parent but that's obviously no use for end users. Commented Aug 18, 2010 at 16:02
  • There is a means to remove an image ... see my full answer below ...
    – EAMann
    Commented Aug 18, 2010 at 19:03

3 Answers 3

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.

6
  • @EAMann: Thanks for that explanation. It would seem however that it's not possible to remove an image from a page and have it removed as an attachment unless you actually delete that image? I was looking for a way to keep images in WordPress' Media Library and be able to add / remove as attachments to Pages. Commented Aug 19, 2010 at 9:17
  • @Rick: If it were available, would a 'move image' function help in your case? I.e. if you could have a holding page/post for all images you want to exist, but not be associated with a 'real' page/post.
    – Bobby Jack
    Commented Aug 19, 2010 at 10:32
  • @EAMann: As a matter of interest, do you know what would happen if you manually set the post_parent to 0 (or NULL, or something)?
    – Bobby Jack
    Commented Aug 19, 2010 at 10:33
  • @Bobby: a 'move image' function could help, although I guess potentially a 'remove image' function would be of more use! Commented Aug 19, 2010 at 10:59
  • 1
    @Bobby: Setting post_parent to 0 removes the attachment from the page, the basic issue is that the only way WP normally does that is when you delete an image from the Media Library. Commented Aug 19, 2010 at 11:02
1

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.

2
  • Thanks for that. I can see that enables a simple text field for entering categories on images, hopefully I can build upon that and make it do what I want. The File Gallery plugin looks good but, as you say, does a lot more than I'm really interested in. But if I can't get what I want to work it may be a good fallback solution. Commented Aug 18, 2010 at 14:44
  • Thanks again for mentioning register_taxonomy_for_object_type(), I can see how it can possibly do what I need but I'm still facing a couple of issues. I'm going to post a new question instead of diluting this one further. Commented Aug 18, 2010 at 16:05
0

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.

5
  • 1
    At first I thought you might be right but I can see within the Media Library listing of images that an image shows as attached to a page even though I've removed it from that page. That would appear buggy to me. Commented Aug 18, 2010 at 14:02
  • Hmmm... I wonder if this depends on how you removed it from the page. Can you see what the value for post_parent is for that attachment in the database?
    – Bobby Jack
    Commented Aug 18, 2010 at 14:36
  • I removed the image from the page simply by hovering over the image and clicking the delete icon. I can see that the post_parent is still set to the ID of the page I just deleted it from. I can't see how this is not a bug, I would have thought that the desired behaviour would be that the parent_page ID should be reset on the image when the image is removed or the page saved. Commented Aug 18, 2010 at 15:29
  • This is not a bug ... it's an important feature. The post_parent field is still set because the image is still attached to the post's gallery even though it's not used in the post's content. Some sites (i.e. mine) use this feature to add images to a post archive page without actually embedding the images in the post's content ... See my full answer for instructions on how to fully delete an image.
    – EAMann
    Commented Aug 18, 2010 at 19:07
  • @EAMann: Thanks, I can see the logic / value in how it works, but as there is no way to remove an image as an attachment without deleting it then it seems flawed to me. Commented Aug 19, 2010 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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