PHP Notice: Function is_page was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false

Your code looked good to me, so I believed that the PHP notice was unlikely caused by the code in question. But that issue could probably be caused by a previous version of that code.

  • E.g. If you had used the init and not wp_enqueue_scripts hook to enqueue your script, then such notice is expected, because WordPress has not yet setup the main query by the the time init runs, (so whether the current request is for a Page, post, category, etc, is not yet known) and then causes WordPress to issue a notice saying <conditional tag> was called incorrectly.

Therefore, I guessed that:

  1. Either your server or WordPress used a cached version of the functions.php or debug.log file.

  2. Or that there’s a plugin or another code in the active theme which caused the issue.

  3. Or maybe you were actually looking at an older entry in the debug log that was no longer valid for the current functions file? (Check the date — maybe it’s August 7th and yet your functions file was edited on August 8th?)

And if it was not the 3rd one above, I would do the following to identify which one is causing the issue:

  • Truncate/empty or just delete your current debug log file, after backing it up if necessary.

  • Purge/clear all caches (server and also browser) — and optionally, temporarily disable caching.

  • Comment out the add-action line, i.e. do //add_action('wp_enqueue_scripts', 'us_homepage_optimize_test');.

  • Save the file and check if the same issue persisted.

If yes, then the code in question was not at fault. But if the issue persisted, then try uncommenting out the add-action line and see if the notice reappears.

So I hope that helps (others), at least in confirming whether that code was the culprit.