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'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
add comment

1 Answer

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 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 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
show 4 more comments

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.