Home › Forums › Calendar Products › Events Calendar PRO › pre_get_post filter spoils ajax navigation
- This topic has 3 replies, 3 voices, and was last updated 9 years, 9 months ago by
Riccardo.
-
AuthorPosts
-
August 7, 2016 at 7:26 am #1148734
Riccardo
ParticipantAfter 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_postin 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!! } }August 7, 2016 at 11:23 am #1148763Riccardo
ParticipantSolved 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;
}
August 7, 2016 at 12:51 pm #1148780Nico
MemberHi 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 -
AuthorPosts
- The topic ‘pre_get_post filter spoils ajax navigation’ is closed to new replies.
