2

I've got code in my functions.php which parses the WP uploads folder and lists all images it finds there. Is it possible to place a filter here that would exclude any image that is already "attached" to a post?

eg: "List all images in folder "uploads" where image is not found in (attached images)"

2
  • Can you specify if you need to list all files on disk or would it be sufficient to refer to those files that are namely stored in the database?
    – hakre
    Commented Nov 10, 2010 at 14:33
  • @hakre: All files on disc that are of type (jpg, gif, png) and are location in folder "wp-content/uploads" (non recursive to subfolders)
    – Scott B
    Commented Nov 10, 2010 at 15:07

1 Answer 1

1

Under Media / Edit, there's a means to list them, so I suggest you reuse the existing API.

Else, a general method, which works anywhere, is to run a db query with a left join:

select ID
  from foo
left join bar
    on whatever
 where condition
   and bar.ID is null;
9
  • @Denis, that was my first thought. Looking at wp-admin/upload.php for clues. Looks like the $lost variable on line 23 might hold clues, yes?... (WP 3.0.1)
    – Scott B
    Commented Nov 10, 2010 at 15:20
  • Woops... Looks like I miss-touched the ipad's screen and deleted your comment. Sorry bout that. The file that does the magic in wp is above. Commented Nov 10, 2010 at 15:22
  • Btw, there also is a meta for each attachment. It contains the path relative to the uploads folder. Commented Nov 10, 2010 at 15:23
  • @Denis, how about this >> run query to collect an array of items in wp_postmeta where meta_key = "_wp_attached_file", then exclude any images from the output stream whose filename is found in the array...
    – Scott B
    Commented Nov 10, 2010 at 15:44

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.