How to list all images in uploads directory except those that are attached to any post
-
16-10-2019 - |
Question
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)"
Solution
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;
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange