Any ajax call to WP will likely call admin-ajax.php as that’s the way you’re meant to make ajax calls in WP. So your last condition will be true for many front end ajax calls and then you’re setting author -1 for all of those queries and excluding everything that User #1, generally the default Admin user, has written.
It’s probably better to use the wp_ajax_query_attachments hook instead which only fires in the context you want and so gives you a much more specific target for your query adjustment:
if ( is_admin() && !in_array( 'administrator', (array) $user->roles ) ) {
add_filter( 'ajax_query_attachments_args', 'exclude_admin_media', 1, 1 );
}
function exclude_admin_media( $query ) {
$query['author'] = '-1';
// exclude admin media
return $query;
}
Related Posts:
- How can I add an Author filter to the Media Library?
- Filter media library items by size
- AJAX filter posts on click based on category
- How can I send data to admin-ajax via JS Fetch?
- How to make post and comment count unclickable with dashboard_glance_items hook
- Change text of Description in Image Library
- Shortcodes not resolved in AJAX call response
- Ajax, filters and shortcodes
- Multiple Ajax Data Action
- Ignore dots when searching in the media library?
- How To Get User Data in Callback Function for pre_user_nicename?
- How to edit dashboard search posts button texts for my CPT?
- Filter Media by Featured on Admin
- Are there actions or filters I can use for Ajax calls?
- How add a group by to the query used by the media library?
- Does a plugin with a AJAX button filter exist? [closed]
- wp_mail works with add_action(‘save_post’, …) but not an ajax action
- Upload restrictions – upload_mimes – filter: Adding multiple MIMEs for a single extension and adding multiple extensions for a single MIME type?
- Is it possible to bind a function to a filter hook via Ajax?
- How To Override A WooCommerce AJAX Function
- Media upload default title from file name
- Create a pre-filtered version of the Media Library
- Filter causing loss of _wp_attachment_metadata
- Best practice: Custom Post Type / Filter / Load more
- Filter Media by attached page or blog post in Library
- Dashboard filter based on custom fields URL response code
- How to add custom media library mime icons?
- Ajax filter – show all results that contain at least one filter
- Removing menus from users other than the administrator
- AJAX Post Filter
- Display post after choice two taxonomy terms
- Ajax Filtering Pagination
- WP Dashboard -> Posts-> Filter > BUG!
- Change add_filter based on Ajax
- Filter posts by categories ajax is showing all the posts
- Testing requested query in pre_get_posts
- WP Dashboard -> Posts-> Filter by Category -> Form Method Change : Which Hook
- Loading a sidebar on an Ajax call
- This code works, but breaks the media uploader. How do I integrate it in a way that won’t?
- Is there a way to override only a portion of the function print_media_templates defined in wp-includes\media-template.php?
- How could I create real-time filtering in this case?
- Load More : Admin Ajax 400 Bad request, returning 0
- Why is the change in my query not taking into account
- How to add headers to outgoing email?
- LESS CSS enqueue_style with add_filter to change rel attribute
- Remove Editor From Homepage
- Remove description from on Home
- How can I detect if a user changes their password?
- Moving sharedaddy buttons (in Jetpack) to the top of a post?
- changing variable through filters or action hooks
- filter the_title problem in nav
- Sensei LMS Hooks to Remove Content
- Where is this inline CSS code [closed]
- When to use actions and when to use filters
- Why won’t my preg_replace work with content_save_pre?
- How Does comment_author Filter Work?
- Add ‘data-text’ attribute to TagCloud HTML
- Using Ajax to load more posts | Help me change the wording on my button to notify the user
- Why in archive page doesn´t show read more button with excerpt?
- Removal of Jetpack OG Tags from old blog site
- get_posts() and filters
- Don’t see add_filter result in the site front page
- How can I include an existing custom sitemaps listing in wp-sitemap.xml?
- Cannot change the definition of AUTOMATIC_UPDATER_DISABLED
- How do I add a checkmark to my-sites save settings hook
- Replace audio links with jplayer using the_content filter
- Sorting a specific taxonomy by archive date using URL
- Adding a filter to comments_template
- How to customize category_description()?
- Filtering a list of objects with ‘OR’ on the same field
- Excerpt is being added above the content, but it should be after it
- How to filter bbPress replies (content)?
- How to add a filter to the get_body_class function?
- Change user nicename without sanitize
- How can I get the default content of WordPress post?
- How do I hide tinymce within the edit screen of a particular page
- What would cause the gettext filter to not work for a given text domain?
- Strip $before & $after strings from a page title
- How does wordpress add ‘style’ attribute to element
- How do I safely force get_theme_mod() to use a defined get_option(“stylesheet”) value?
- Shortcode / plugin with custom (flexible) output
- Is it better to use a constant or apply_filter?
- Ajax Post Filter | Split Taxonomy Into minPrice & maxPrice
- How to add HTML into error message
- add_action with associative array
- Remove image of srcset
- Add option to query string before get_posts() is called on archive.php
- Modify wp headers on specific page
- How to make the show as a button?
- Why this remove empty paragraphs from the_content does not works?
- Take filter from multiple functions
- Read More in the actual excerpt
- What scope is $blog_url = get_bloginfo() inside a function
- How we make the filter for fornt end user
- Filter dashboard custom post listing by user
- How to filter part of a variable if it is no array?
- WordPress wp_lazy_loading_enabled returns loading attribute set to lazy
- Deregister Custom the_title Filter for edit_post_link
- How to apply_filter / add_filter within 2 others (simultaneous?) add_filter
- How to override hook from themes functions.php which is defined in plugin