{"id":1148734,"date":"2016-08-07T07:26:23","date_gmt":"2016-08-07T14:26:23","guid":{"rendered":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/"},"modified":"2016-08-07T07:26:23","modified_gmt":"2016-08-07T14:26:23","slug":"pre_get_post-filter-spoils-ajax-navigation","status":"closed","type":"topic","link":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/","title":{"rendered":"pre_get_post filter spoils ajax navigation"},"content":{"rendered":"<p>After digging in the code I have realised that a pre_get_post filter in my <code>functions.php<\/code> (not related to the calendar) is spoiling the default behaviour of the calendar when using the navigation links to next\/prev month.<\/p>\n<p>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 <code>pre_get_post<\/code> in a way that when non admin users are accessing the media library, content will be filtered. This runs perfectly.<\/p>\n<p>The problem occurs because this filter is applied to the dashboard media library loading module (namely <code>admin-ajax.php<\/code>) 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.<\/p>\n<p>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. <\/p>\n<p>Here&#8217;s the offending code in functions.php:<\/p>\n<pre><code>$url_split = explode(&quot;\/&quot;,$_SERVER[&#039;REQUEST_URI&#039;]); \/\/ get current URI to ensure the filter is only applied when navigating in the dashboard\n\n$user = wp_get_current_user();   \nif ( is_admin() &amp;&amp; !in_array( &#039;administrator&#039;, (array) $user-&gt;roles ) &amp;&amp; strtolower($url_split[1]) == &quot;wp-admin&quot;){\n    add_filter(&#039;pre_get_posts&#039;, &#039;posts_for_current_role&#039;);\n}\n\nfunction posts_for_current_role($query) {\n        if( in_array( $pagenow, array( &#039;upload.php&#039;, &#039;admin-ajax.php&#039;)  ) ){\n            \/\/ hide admin media for non-admin users\n            $query-&gt;set(&#039;author&#039;,  &#039;-1&#039;); \/\/ offending statement, spoils the calendar navigation!!\n        }\n    \n}<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-1148734","topic","type-topic","status-closed","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>pre_get_post filter spoils ajax navigation -<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"pre_get_post filter spoils ajax navigation -\" \/>\n<meta property=\"og:description\" content=\"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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/\",\"url\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/\",\"name\":\"pre_get_post filter spoils ajax navigation -\",\"isPartOf\":{\"@id\":\"https:\/\/theeventscalendar.com\/support\/#website\"},\"datePublished\":\"2016-08-07T14:26:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/theeventscalendar.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Topics\",\"item\":\"https:\/\/theeventscalendar.com\/support\/topics\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Calendar Products\",\"item\":\"https:\/\/theeventscalendar.com\/support\/forums\/forum\/events\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Events Calendar PRO\",\"item\":\"https:\/\/theeventscalendar.com\/support\/forums\/forum\/events\/events-calendar-pro\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"pre_get_post filter spoils ajax navigation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/theeventscalendar.com\/support\/#website\",\"url\":\"https:\/\/theeventscalendar.com\/support\/\",\"name\":\"\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/theeventscalendar.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"pre_get_post filter spoils ajax navigation -","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/","og_locale":"en_US","og_type":"article","og_title":"pre_get_post filter spoils ajax navigation -","og_description":"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 [&hellip;]","og_url":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/","url":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/","name":"pre_get_post filter spoils ajax navigation -","isPartOf":{"@id":"https:\/\/theeventscalendar.com\/support\/#website"},"datePublished":"2016-08-07T14:26:23+00:00","breadcrumb":{"@id":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/pre_get_post-filter-spoils-ajax-navigation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/theeventscalendar.com\/support\/"},{"@type":"ListItem","position":2,"name":"Topics","item":"https:\/\/theeventscalendar.com\/support\/topics\/"},{"@type":"ListItem","position":3,"name":"Calendar Products","item":"https:\/\/theeventscalendar.com\/support\/forums\/forum\/events\/"},{"@type":"ListItem","position":4,"name":"Events Calendar PRO","item":"https:\/\/theeventscalendar.com\/support\/forums\/forum\/events\/events-calendar-pro\/"},{"@type":"ListItem","position":5,"name":"pre_get_post filter spoils ajax navigation"}]},{"@type":"WebSite","@id":"https:\/\/theeventscalendar.com\/support\/#website","url":"https:\/\/theeventscalendar.com\/support\/","name":"","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/theeventscalendar.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic\/1148734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic\/1148734\/revisions"}],"wp:attachment":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/media?parent=1148734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}