The issue seems to arise because the pre_get_posts
hook you’re using to filter media library queries might be affecting other queries as well, and your filtering condition isn’t targeting the exact query you’re trying to modify. Here’s a detailed breakdown and a solution:
Key Points to Address:
- Scope the Query Correctly: Ensure your
pre_get_posts
filter applies only to the media library query. - Verify
is_admin()
and Media-Specific Requests: Media library queries can have specific indicators, likeis_admin()
and$_REQUEST['action'] == 'query-attachments'
. - Plugins Interference: ACF Pro and FileBird might interfere by adding their custom hooks or modifying queries.
Updated Code
Here’s an improved version of your function to scope it correctly:
add_action( 'pre_get_posts', 'coapp_restrict_media_library' );
function coapp_restrict_media_library( $query ) {
// Only apply the filter in admin area for AJAX media library requests
if ( ! is_admin() || ! isset( $_REQUEST['action'] ) || $_REQUEST['action'] !== 'query-attachments' ) {
return;
}
// Avoid filtering queries for users with higher permissions
if ( current_user_can('edit_pages') ) {
return;
}
// Check if it's a media library request
$post_type = $query->get('post_type');
if ( $post_type === 'attachment' ) {
error_log('change author__in'); // Debugging log
$query->set( 'author__in', [ get_current_user_id() ] ); // Replace $ids with current user ID for simplicity
}
error_log( print_r( $query, true ) ); // Debugging log
}
Explanation of Changes:
-
Targeting Media Library Requests:
$_REQUEST['action'] === 'query-attachments'
ensures this code only runs for media library AJAX requests.is_admin()
ensures the filter runs only in the WordPress admin panel.
-
Scope to Attachments:
- The condition
$post_type === 'attachment'
checks if the query is for theattachment
post type, which corresponds to media library queries.
- The condition
-
Replace
$ids
withget_current_user_id()
:- The original
$ids
was undefined in your code snippet. If you want to restrict media to the current user, you can useget_current_user_id()
.
- The original
-
Error Logging:
- Logs like
error_log('change author__in');
anderror_log( print_r( $query, true ) );
help debug the query.
- Logs like
Debugging Steps:
-
Check Query Modification:
- Use the error logs to ensure the
author__in
parameter is being set correctly in the query.
- Use the error logs to ensure the
-
Disable Plugins Temporarily:
- Deactivate ACF Pro and FileBird temporarily to confirm whether they interfere with media queries.
-
Verify AJAX Request:
- Open the browser’s developer tools, navigate to the “Network” tab, and check the request parameters for
query-attachments
to ensure the AJAX request aligns with the condition.
- Open the browser’s developer tools, navigate to the “Network” tab, and check the request parameters for
Possible Plugin Interference:
-
ACF Pro or FileBird:** These plugins might hook into
pre_get_posts
or modify media library queries. If this is confirmed, you might need to prioritize your hook by adjusting the priority:add_action( 'pre_get_posts', 'coapp_restrict_media_library', 20 );
-
Alternatively, check for conflicts in their documentation or support forums.
This code should address your issue by properly scoping the pre_get_posts
filter to the specific media library query. Let me know if further debugging is required!