WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)"

share|improve this question
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 Nov 10 '10 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 Nov 10 '10 at 15:07
up vote 1 down vote accepted

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;
share|improve this answer
@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 Nov 10 '10 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. – Denis de Bernardy Nov 10 '10 at 15:22
Btw, there also is a meta for each attachment. It contains the path relative to the uploads folder. – Denis de Bernardy Nov 10 '10 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 Nov 10 '10 at 15:44

Your Answer


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.