How do I detect in which page ajax_query_attachments_args is loaded?

Question

I’m trying to hide attachments with a taxonomy. I want to hide them from Post page in wp-admin. I can’t figure out how to detect the page I’m in from ajax_query_attachments_args. I tried to use pre_get_posts, but it doesn’t seem to trigger ajax_query_attachments_args.

add_filter('ajax_query_attachments_args', 'hide_attachment_with_taxonomy', 50, 1);  

function hide_attachment_with_taxonomy( $query = array() ) {
        global $current_screen;

        if ( ! $current_screen ) {
            return $query; // It always return this value. $current_screen or get_current_screen() is always undefined
        }


        if($current_screen->post_type == 'post'){
          $query['tax_query'] = array(
            array(
              'taxonomy' => 'category',
              'operator' => 'NOT EXISTS'
            ),
          ); 

          return $query;
       }
}
0
Nomadic Julien 4 years 2019-11-05T12:37:09-05:00 0 Answers 57 views 0

Leave an answer

Browse
Browse