as the title implies - I would like to suppress attachments of a specific post type(s) from showing up in the "Media Library"

Not asking for a code example, but if someone can point me to the right hook it would be much appreciated. Thanks!

  • 2
    I recall @KrzysiekDróżdż doing something similar here
    – birgire
    Jul 17 '15 at 20:56
  • @birigire -this was extraordinarily helpful. Wasn't exactly what I wanted to accomplish but did outline how to hook into the proper process. Thanks! Jul 17 '15 at 22:32

For the media library you can use the pre_get_posts hook targeting upload.php by means of $pagenow, a global variable, or even better, because globals are the devils child, by means of get_current_screen(). I'm assuming you want to target the media modal tab too - because targeting only one, but not the other, doesn't make much sense after all -, you can do that via the ajax_query_attachments_args hook.

  • Ok great. I'll look into this and will come back and accept the answer if this works out. Thanks! Jul 17 '15 at 19:15
  • My pleasure, and sure, just take your time. One more hint, you are looking for the attachments parents - post_parent -, which are the posts the files have been uploaded to, of course you have to sort out the correct ones, by post type in your case. If you have those, preferably an array of ids, then you can exclude them from the query. @BrandtSolovij
    – Nicolai
    Jul 17 '15 at 19:20
  • thanks again - mostly we have some version control around posttypes and some are focused around attachments - those attachments have found their way into the media library "browse" modals. I simply wish to suppress those - so get_current_screen + pre_get_posts right now feels like the fit @ialocin Jul 17 '15 at 19:30
  • No problem, what I said isn't mutually exclusive, on the contrary, you need to do both, determine what you want to exclude and then target the query to actually exclude, in some way or another. Ping me if you are not getting there, but I won't be able to answer by sometime next week. @BrandtSolovij
    – Nicolai
    Jul 17 '15 at 19:37
  • 1
    ajax_query_attachments_args was where to start. Thanks! Jul 17 '15 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.