pre_get_post filter spoils ajax navigation

Home Forums Calendar Products Events Calendar PRO pre_get_post filter spoils ajax navigation

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1148734
    Riccardo
    Participant

    After digging in the code I have realised that a pre_get_post filter in my functions.php (not related to the calendar) is spoiling the default behaviour of the calendar when using the navigation links to next/prev month.

    What I want to achieve is to filter media library content in the dashboard to prevent non admin content to appear in the media library. So I have setup a filter pre_get_post in a way that when non admin users are accessing the media library, content will be filtered. This runs perfectly.

    The problem occurs because this filter is applied to the dashboard media library loading module (namely admin-ajax.php) and for unknown reasons it looks like the filter is being applied also when the calendar navigation is called! This filter should be applied only when the user is in the backend, but for some unknown reasons the code is being run also when the user is navigating in the frontend (while logged in), and the calendar navigation will suffer from this.

    I wonder if is there a solution to stop this. I have attempted to stuff as much as possible conditions to stop running the code when user is not in the dashboard but it looks like somehow it is running.

    Here’s the offending code in functions.php:

    $url_split = explode("/",$_SERVER['REQUEST_URI']); // get current URI to ensure the filter is only applied when navigating in the dashboard
    
    $user = wp_get_current_user();   
    if ( is_admin() && !in_array( 'administrator', (array) $user->roles ) && strtolower($url_split[1]) == "wp-admin"){
        add_filter('pre_get_posts', 'posts_for_current_role');
    }
    
    function posts_for_current_role($query) {
            if( in_array( $pagenow, array( 'upload.php', 'admin-ajax.php')  ) ){
                // hide admin media for non-admin users
                $query->set('author',  '-1'); // offending statement, spoils the calendar navigation!!
            }
        
    }
    #1148763
    Riccardo
    Participant

    Solved replacing the code for the media library:


    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;
    }

    #1148780
    Nico
    Member

    Hi there Riccardo,

    Thanks for getting in touch and for giving us a heads-up on the solution for this issue 🙂

    I’ll go ahead and close out this thread, but if you need help with anything else please don’t hesitate to create a new one and we will be happy to assist you.

    Best,
    Nico

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘pre_get_post filter spoils ajax navigation’ is closed to new replies.