How to list all images in uploads directory except those that are attached to any post

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

Related posts

Leave a Reply

1 comment

  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;