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?
- Changing WooCommerce Display Price Based on User Role & Category [closed]
- LESS CSS enqueue_style with add_filter to change rel attribute
- Remove Editor From Homepage
- What do add_filters() and apply_filter() do?
- What to use instead of the content_save_pre filter?
- Moving sharedaddy buttons (in Jetpack) to the top of a post?
- Ajax filter with custom taxonomies
- How can I customize the wp_list_categories
- How can I reliably and globally disable wptexturize?
- Add classname to the_category() function?
- Filter the URL of next_posts_link & previous_posts_link
- Sensei LMS Hooks to Remove Content
- How to re-enable a filter after disabling with __return_false
- Exclude One Category and its Subcategories using WP_LIST_FILTER
- Gutenberg disable the “block” tab in right sidebar
- Where is this inline CSS code [closed]
- When to use actions and when to use filters
- add_filter to ‘woocommerce_before_main_content’ [closed]
- Editing
- Echoing Categories only if they’re a child of a given category
- Why in archive page doesn´t show read more button with excerpt?
- Removal of Jetpack OG Tags from old blog site
- Adding a title prefix with the the_title filter
- Don’t see add_filter result in the site front page
- How can I include an existing custom sitemaps listing in wp-sitemap.xml?
- Display Externally Retrieved Data within a Custom Template
- Can the wordpress color palettes by changed through Javascript?
- Filter on the_content doesn’t update the content being searched via register_rest_route
- Where to add_filter in theme functions.php?
- Cannot change the definition of AUTOMATIC_UPDATER_DISABLED
- tag removed not using a filter
- WordPress Image update filter
- How to remove this filter?
- Load Posts’ individual body content on index starting at char 200 of each post
- How to combine multiple body classes
- How to replace username with email address in users table
- why require – does not load filter
- How can I filter tag_description()?
- How to hide some categories in dashboard
- Need to return shortcode text instead of the output
- How do I make these combination select filters work when only one dropdown is set? They work in WordPress 5.8.2 but don’t in 5.8.3
- I want to add string array to this function to filter displayed option in drpodown [closed]
- Function not returning correct value for found_posts filter
- Remove style tags from head
- How to wrap pattern-matches from the_content in [wiki]-shortcode tags
- how to develop a filter in wordpress to let the user filter the page content depends on the date (newest to oldest etc…)?
- Print only parent categories of post in custom RSS feed
- Use add_filter inside another function
- Ajax response returns html code instead of user data
- How to filter reading/visibility posts based on role?
- Admin view: filter posts by whether they have specific taxonomy attached
- URL to filter posts by post meta similar to shop/?product_cat=”painting”
- Incrementing content with extra text after save/publish
- How to apply a filter to everything?
- WP Job Manager – display search results from custom search form in taxonomy-job_listing_category page
- Conditionally (cpt) filter previous and next_post_link
- changing size image within the content
- Identify admin/logout and more filters
- Sanitizing a custom query’s clauses
- How can I display wp_link_pages before a shortcode, if it is used, or display after content?
- How to create a list of terms who’s posts also have a predefined external term?