Why is the change in my query not taking into account

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:

  1. Scope the Query Correctly: Ensure your pre_get_posts filter applies only to the media library query.
  2. Verify is_admin() and Media-Specific Requests: Media library queries can have specific indicators, like is_admin() and $_REQUEST['action'] == 'query-attachments'.
  3. 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:

  1. 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.
  2. Scope to Attachments:

    • The condition $post_type === 'attachment' checks if the query is for the attachment post type, which corresponds to media library queries.
  3. Replace $ids with get_current_user_id():

    • The original $ids was undefined in your code snippet. If you want to restrict media to the current user, you can use get_current_user_id().
  4. Error Logging:

    • Logs like error_log('change author__in'); and error_log( print_r( $query, true ) ); help debug the query.

Debugging Steps:

  1. Check Query Modification:

    • Use the error logs to ensure the author__in parameter is being set correctly in the query.
  2. Disable Plugins Temporarily:

    • Deactivate ACF Pro and FileBird temporarily to confirm whether they interfere with media queries.
  3. 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.

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!

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)