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?
- Why wordpress doesn’t allow to add author filter to media library page in grid mode
- 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
- Disable emojicons introduced with WP 4.2
- How to remove a filter that is an anonymous object?
- WordPress hooks/filters insert before content or after title
- How to add defer=”defer” tag in plugin javascripts?
- add_action(), add_filter() before or after function
- apply_filters(‘the_content’, $content) vs do_shortcode($content)
- How do filters and hooks really work in PHP
- Trouble understanding apply_filters()
- What is the very earliest action hook you can call?
- How would one modify the filtering Gutenberg applies to pasted content?
- How can I modify the WordPress default widget output?
- Add custom options to the wplink dialog
- Remove classes from body_class
- what is __return_false in filters
- Explanation for apply_filters function and its variables
- Gutenberg: Is there a way to know if current block is inside InnerBlocks?
- How to reorder billing fields in WooCommerce Checkout template? [closed]
- Insert HTML just after tag
- the_content and is_main_query
- Changing WooCommerce Display Price Based on User Role & Category [closed]
- How to add a custom CSS class to core blocks in Gutenberg editor?
- How to show page content in feed?
- wp_headers vs send_headers. When to use each?
- Filter any HTTP request URI?
- How to Pass External Variables to Filters/Actions
- How to filter users on admin users page by custom meta field?
- Filter by one custom field, order by another?
- Media library – Limit images to custom post type
- Not able to change wp_title using add_filter
- Why not register shortcodes if is_admin dashboard?
- How to appending to the_content using add_filter with custom post type?
- Query WP REST API v2 by multiple meta keys
- No filter of code on switch from html to visual editor, how?
- Sanitize and data validation with apply_filters() function
- How to modify posts_where filter only for the search query
- How to hook a filter to catch get_post_meta when alternate a custom field output?
- How to get shortcode’s input values inside a filter?
- Removing Image and Caption Dimension Attributes
- How to wrap oEmbed-embedded video in DIV tags inside the_content?
- How to bulk delete all users with no posts?
- How many filter/action hooks are healthy?
- WordPress 3.9 – Trouble Editing TinyMCE 4.0
- Changing JPEG compression depending on image size
- How to add filter with 2 args?
- Why is javascript allowed in my post content?
- How to use update_{$meta_type}_metadata filter to modify meta value
- AJAX with loop filtering categories
- How to wrap an element around an iframe or embed in content automatically?
- Filter specific shortcode output?
- WordPress Internal @ Mentions
- How to add headers to outgoing email?
- Earliest hook to reliably get $post/$posts
- Insert new element to array with add_filter
- LESS CSS enqueue_style with add_filter to change rel attribute
- Is it possible to filter comments in a post so a user can only see the comments they have written?
- How to pass/get data to/from the WooCommerce data-product_variations object?
- Remove Editor From Homepage