Request for Added Cache Routine Check
-
Hi Folks,
Two days ago I noticed thousands of the following two Error Messages in my WP debug.log as follows. (My debug log is normally off, but I was working on some other issue when this came to light.)
[27-May-2026 07:11:08 UTC] PHP Notice: Function is_search was called <strong>incorrectly</strong>. Conditional query tags do not work before the query is run. Before then, they always return false. Please see <a href="https://developer.ww.wp.xz.cn/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 3.1.0.) in /xxx/wp-includes/functions.php on line 6170[27-May-2026 07:11:08 UTC] PHP Notice: Function is_404 was called <strong>incorrectly</strong>. Conditional query tags do not work before the query is run. Before then, they always return false. Please see <a href="https://developer.ww.wp.xz.cn/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 3.1.0.) in /xxx/wp-includes/functions.php on line 6170We have recently updated to WP7.0 and after checking we found the Kadence Security Basic (KSB) apparently related to the issue.
After further testing and exchanges with KSB, we obtained the following information on the issue which related to WP-Optimize :
[28-May-2026 10:31:22 UTC] [conditional-tag-backtrace] is_search | shutdown_action_hook, do_action('shutdown'), WP_Hook->do_action, WP_Hook->apply_filters, wp_ob_end_flush_all, ob_end_flush, WPO_Page_Optimizer->optimize, WPO_Page_Optimizer->maybe_cache_page, WPO_Page_Cache->should_cache_page, wpo_can_serve_from_cache, wpo_restricted_cache_page_type, wpo_is_search, is_search, _doing_it_wrong, do_action('doing_it_wrong_run'), WP_Hook->do_action, WP_Hook->apply_filters, {closure}[28-May-2026 10:31:22 UTC] PHP Notice: Function is_search was called <strong>incorrectly</strong>. Conditional query tags do not work before the query is run. Before then, they always return false. Please see <a href="https://developer.ww.wp.xz.cn/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 3.1.0.) in /xxx/wp-includes/functions.php on line 6170[28-May-2026 10:31:22 UTC] [conditional-tag-backtrace] is_404 | shutdown_action_hook, do_action('shutdown'), WP_Hook->do_action, WP_Hook->apply_filters, wp_ob_end_flush_all, ob_end_flush, WPO_Page_Optimizer->optimize, WPO_Page_Optimizer->maybe_cache_page, WPO_Page_Cache->should_cache_page, wpo_can_serve_from_cache, wpo_restricted_cache_page_type, is_404, _doing_it_wrong, do_action('doing_it_wrong_run'), WP_Hook->do_action, WP_Hook->apply_filters, {closure}[28-May-2026 10:31:22 UTC] PHP Notice: Function is_404 was called <strong>incorrectly</strong>. Conditional query tags do not work before the query is run. Before then, they always return false. Please see <a href="https://developer.ww.wp.xz.cn/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 3.1.0.) in /xxx/wp-includes/functions.php on line 6170[28-May-2026 10:31:22 UTC] PHP Notice: Function is_feed was called <strong>incorrectly</strong>. Conditional query tags do not work before the query is run. Before then, they always return false. Please see <a href="https://developer.ww.wp.xz.cn/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 3.1.0.) in /xxx/functions.php on line 6170And finally from our Server Error Log:
[Thu May 28 21:20:36.567391 2026] [proxy_fcgi:error] [pid 1078998:tid 1079117] [client 144.76.23.106:50274] AH01071: Got error 'PHP message: [conditional-tag-backtrace] is_search | shutdown_action_hook, do_action('shutdown'), WP_Hook->do_action, WP_Hook->apply_filters, wp_ob_end_flush_all, ob_end_flush, WPO_Page_Optimizer->optimize, WPO_Page_Optimizer->maybe_cache_page, WPO_Page_Cache->should_cache_page, wpo_can_serve_from_cache, wpo_restricted_cache_page_type, wpo_is_search, is_search, _doing_it_wrong, do_action('doing_it_wrong_run'), WP_Hook->do_action, WP_Hook->apply_filters, {closure}; PHP message: [conditional-tag-backtrace] is_404 | shutdown_action_hook, do_action('shutdown'), WP_Hook->do_action, WP_Hook->apply_filters, wp_ob_end_flush_all, ob_end_flush, WPO_Page_Optimizer->optimize, WPO_Page_Optimizer->maybe_cache_page, WPO_Page_Cache->should_cache_page, wpo_can_serve_from_cache, wpo_restricted_cache_page_type, is_404, _doing_it_wrong, do_action('doing_it_wrong_run'), WP_Hook->do_action, WP_Hook->apply_filters, {closure}'KSB’s explanation of the situation was: “What is happening is that WP-Optimize, when it decides at the very end of the request whether the page is cacheable, calls is_search(), is_404() and is_feed() to rule out search/404/feed pages. Those three core functions print the “called incorrectly” notice whenever they run before WordPress has built its main query object, and that is precisely the situation here.
Here is why Kadence Security flips it on and off for you, even though the plugin never calls those functions itself:
Your firewall is set to load early (it boots before the rest of WordPress so it can stop attacks before they reach your site). When the firewall blocks a request, or when a repeat-offender IP is already locked out, it ends that request immediately, before WordPress gets far enough to build its query object. WP-Optimize, however, opens its page-cache output buffer even earlier than that, so its end-of-request “should I cache this page?” check still runs on the way out. At that point the query never ran, so is_search()/is_404()/is_feed() correctly report that they were called too early, and the notice is logged. With Kadence Security switched off, nothing ends those requests early, the query always runs, and WP-Optimize’s check has no reason to complain. That is the whole interaction.”
The final comment from KSB suggests that your cache routine should check that the main query has actually run (did_action(‘wp’)) before calling those conditional tags. And this would permanently resolve the matter.
I leave this in your hands now and humble ask that you look at the suggestion from KSB.
If there is anything else you need from us to help resolve this issue, please do just ask. Always pleased to help.
For more information on our exchanges with KSB, please see: https://ww.wp.xz.cn/support/topic/wp7-0-issue-with-functions-php-line-6170/#post-18922870
Regards and thanks,
AngusThe page I need help with: [log in to see the link]
You must be logged in to reply to this topic.