Skip Navigation

[Escalated to 2nd Tier] is_search, is_home, is_archive called incorrectly

This support ticket is created 3 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 17 replies, has 6 voices.

Last updated by NickC8880 3 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1998695

Hello, when using Toolset (Blocks or Views) with WooCommerce and WooCommerce Memberships, 3 PHP Notices are logged for every single page load.

The errors are

PHP Notice:  is_archive 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://wordpress.org/support/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 3.1.0.) in .../wp-includes/functions.php on line 5313
PHP Notice:  is_home 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://wordpress.org/support/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 3.1.0.) in .../wp-includes/functions.php on line 5313
PHP Notice:  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://wordpress.org/support/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 3.1.0.) in .../wp-includes/functions.php on line 5313

Initially I suspected WooCommerce Memberships but the error is not due to their plugin (they do not even use 2 of the failing functions, only one).
It happens only when you also use Toolset (Blocks or Views)
The reason for the error is that in Toolset (Views or Blocks) file .../toolset-blocks/embedded/inc/wpv-api.php lines 346 until 348 call the respective functions without first checking if the WP_Query instance is actually set.

if (
		! is_archive()
		&& ! is_home()
		&& ! is_search()
	) {
		return false;
	}

I am not sure how you can fix this, however, the error is explicitly only happening when Toolset Blocks is active and there have been several other reports for other plugins "doing it wrong" in the WP Forums exactly in relation with WooCommerce Memberships.
It seems, something clashes when calling the templates tags is_archive, is_home, is_search like Toolset (and others) do in certain circumstances which I am not in the position to debug, but the Error log is pretty clear too (posting the one for is_archive() only as the others are identical):

[24-Mar-2021 07:11:54 UTC] PHP Notice:  is_archive 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://wordpress.org/support/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 3.1.0.) in .../wp-includes/functions.php on line 5313
[24-Mar-2021 07:11:54 UTC] PHP Stack trace:
[24-Mar-2021 07:11:54 UTC] PHP   1. {main}() .../index.php:0
[24-Mar-2021 07:11:54 UTC] PHP   2. require() .../index.php:17
[24-Mar-2021 07:11:54 UTC] PHP   3. require_once() .../wp-blog-header.php:13
[24-Mar-2021 07:11:54 UTC] PHP   4. require_once() .../wp-load.php:37
[24-Mar-2021 07:11:54 UTC] PHP   5. require_once() .../wp-config.php:93
[24-Mar-2021 07:11:54 UTC] PHP   6. do_action() .../wp-settings.php:423
[24-Mar-2021 07:11:54 UTC] PHP   7. WP_Hook->do_action() .../wp-includes/plugin.php:484
[24-Mar-2021 07:11:54 UTC] PHP   8. WP_Hook->apply_filters() .../wp-includes/class-wp-hook.php:316
[24-Mar-2021 07:11:54 UTC] PHP   9. WC_Memberships->init_plugin() .../wp-includes/class-wp-hook.php:292
[24-Mar-2021 07:11:54 UTC] PHP  10. WC_Memberships->includes() .../wp-content/plugins/woocommerce-memberships/class-wc-memberships.php:161
[24-Mar-2021 07:11:54 UTC] PHP  11. WC_Memberships->frontend_includes() .../wp-content/plugins/woocommerce-memberships/class-wc-memberships.php:282
[24-Mar-2021 07:11:54 UTC] PHP  12. WC_Memberships->load_class() .../wp-content/plugins/woocommerce-memberships/class-wc-memberships.php:355
[24-Mar-2021 07:11:54 UTC] PHP  13. WC_Memberships_Frontend->__construct() .../wp-content/plugins/woocommerce-memberships/vendor/skyverge/wc-plugin-framework/woocommerce/class-sv-wc-plugin.php:810
[24-Mar-2021 07:11:54 UTC] PHP  14. WC_Memberships->load_class() .../wp-content/plugins/woocommerce-memberships/includes/frontend/class-wc-memberships-frontend.php:61
[24-Mar-2021 07:11:54 UTC] PHP  15. SkyVerge\WooCommerce\Memberships\Frontend\My_Account->__construct() .../wp-content/plugins/woocommerce-memberships/vendor/skyverge/wc-plugin-framework/woocommerce/class-sv-wc-plugin.php:810
[24-Mar-2021 07:11:54 UTC] PHP  16. WC_Memberships->load_class() .../wp-content/plugins/woocommerce-memberships/includes/frontend/My_Account.php:53
[24-Mar-2021 07:11:54 UTC] PHP  17. SkyVerge\WooCommerce\Memberships\Frontend\My_Account\Members_Area->__construct() .../wp-content/plugins/woocommerce-memberships/vendor/skyverge/wc-plugin-framework/woocommerce/class-sv-wc-plugin.php:810
[24-Mar-2021 07:11:54 UTC] PHP  18. wc_memberships_get_members_area_endpoint() .../wp-content/plugins/woocommerce-memberships/includes/frontend/My_Account/Members_Area.php:69
[24-Mar-2021 07:11:54 UTC] PHP  19. wc_memberships_get_user_memberships() .../wp-content/plugins/woocommerce-memberships/includes/functions/wc-memberships-functions-members-area.php:93
[24-Mar-2021 07:11:54 UTC] PHP  20. WC_Memberships_User_Memberships->get_user_memberships() .../wp-content/plugins/woocommerce-memberships/includes/functions/wc-memberships-functions-user-memberships.php:56
[24-Mar-2021 07:11:54 UTC] PHP  21. get_posts() .../wp-content/plugins/woocommerce-memberships/includes/class-wc-memberships-user-memberships.php:322
[24-Mar-2021 07:11:54 UTC] PHP  22. WP_Query->query() .../wp-includes/post.php:2153
[24-Mar-2021 07:11:54 UTC] PHP  23. WP_Query->get_posts() .../wp-includes/class-wp-query.php:3465
[24-Mar-2021 07:11:54 UTC] PHP  24. do_action_ref_array() .../wp-includes/class-wp-query.php:1784
[24-Mar-2021 07:11:54 UTC] PHP  25. WP_Hook->do_action() .../wp-includes/plugin.php:551
[24-Mar-2021 07:11:54 UTC] PHP  26. WP_Hook->apply_filters() .../wp-includes/class-wp-hook.php:316
[24-Mar-2021 07:11:54 UTC] PHP  27. WPV_WordPress_Archive_Frontend->early_pre_get_posts() .../wp-includes/class-wp-hook.php:292
[24-Mar-2021 07:11:54 UTC] PHP  28. is_wpv_wp_archive_assigned() .../wp-content/plugins/toolset-blocks/embedded/inc/wpv-archive-loop.php:2159
[24-Mar-2021 07:11:54 UTC] PHP  29. is_archive() .../wp-content/plugins/toolset-blocks/embedded/inc/wpv-api.php:347
[24-Mar-2021 07:11:54 UTC] PHP  30. _doing_it_wrong() .../wp-includes/query.php:164
[24-Mar-2021 07:11:54 UTC] PHP  31. trigger_error() .../wp-includes/functions.php:5313

Note that this is replicable easily on a fresh install.
I have installed Blocks (or Views), WC and WC Memberships (no configuration needed) and simply load the front end.

It would be important to fix this because the error (while just a notice) logs for each page load 3 lines.
On a busy site, that makes it a gb sized error log by the end of day. And sites can be busy because one is developing, not just because of user visits, on live sites we would then just disable the error logging.

Also to denote is that the (or similar) error has been reported previously:
https://toolset.com/forums/topic/cant-modify-header-using-wordpress-5-6-1/
https://toolset.com/forums/topic/create-form-fields-dynamically-based-on-term-count/page/2/

Thank you for looking into this.

#1998795

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Beda

Seems like we don't have a copy of the membership plugin to hand. Are you able to share a zipped copy for testing? Then I can get right on to it, otherwise I'll ask the compatibility team to contact the authors.

#1998903

Hi Nigel

Here we go
hidden link

#2000871

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Thanks Beda

I looked into this, and I don't think it's on us.

We hook into pre_get_posts and use those template tags, such as is_archive(), but the error logs indicate they are not ready to be used yet.

If you activate just WooCommerce, plus Query Monitor, and load the home page on the front end, and check the action hooks, you'll see that they load in the familiar order you are probably used to, very abbreviated highlights of which would be

setup_theme
init
wp_loaded
parse_request
parse_query
pre_get_posts
...
wp

Now activate WC Memberships (still no Toolset here), and repeat. This time you end up with something like...

parse_query
pre_get_posts
setup_theme
init
wp_loaded
parse_request
...
wp

See how the plugin causes pre_get_posts to be triggered much earlier in the cycle of loading WordPress, even before the init action.

I think it's to be expected that if you move such a key hook it will have unintended consequences, and is likely why you say there are other reports of "doing it wrong" related to the plugin.

I think you should contact them about stopping whatever it is they do that relocates the position of pre_get_posts in the loading cycle.

#2000921

Thanks Nigel for the additional debug

I didn’t notice this happens... have contacted them back and will see if we can get something solved

I think these plugins are both used often enough tgt to make an effort in compatibility... let’s see if their support/dev may want to cooperate

Can we keep it open for now? I’ve shared this tick with them so they could use it as a reference

Cheers...

#2001923

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sure, no problem, I'll check back in a week or two to see if you have feedback and we can close.

#2003447

Hello Nigel, I was trying to craft a POC for the WC Memberships Support, because if the issue is "they do it wrong" then it should not require a paid Plugin to trigger the issue (toolset in this case).
Doing so I struggled. I am not convinced the issue is only on them, but rather on the plugin calling the tags.

See below what I found.

If we add a very simple code to our Theme that simply checks if is_home, is_search or is_archive on pre_get_posts, and enable the WC + WC Membership plugins the error won't happen at all.

function hook_into_pre_get_posts_with_template_tag( $query ) {
 if(is_search() || is_home() || is_archive()){
 	error_log( 'Is home, or search or archive' );
 }
 else{
 	error_log( 'anything else' );
 }
}
add_action( 'pre_get_posts', 'hook_into_pre_get_posts_with_template_tag' );

When we call for example site.com/?s=anything, with this code in the theme, we get the proper Debug log (Is home, or search or archive) and no error.

We could even (but it is not necessary to) check "if ( is_admin() || !$query->is_main_query() ) {" which is pretty much what WP Core "doing it wrong" notice suggests in this case (to check if the query is ready), as far I understand.

But again, it is not needed. The error is not happening with this simple code.

So this brought me to the idea of trying this in a plugin, because since those are loaded in a different hook than a theme file, it could be happening there - and indeed.
As soon you call the exact same code in a plugin the error will happen!

And it is solved by adding this line of code which is what WP suggests to do anyway, and as far I see, Toolset does not do it:

if ( is_admin() || !$query->is_main_query() ) {
  return;
}

This resolved the issue for me, and my code still works.

I actually had a plugin lying around that I made myself and it uses another hook (parse_query) and I run into the precise same error lol...

So I fixed it with adding the check if is_admin and if is_main_query and it was solved and the logic is still working, because logically, it can and will fire as soon the query is ready anyway. And that is all we need.
So I think toolset has to review the code where it hooks into pre_get_posts without checking if the query is actually ready.
This is also what the WP Core message indeed suggests under the hood, without explicitly saying it, they say "it may not be ready so you need to check". The 'location' of pre_get_query or other query hooks are not mandatory.

To summarise the POC is:
1. Create a simple WP Plugin
2. In the main plugin file or any class of the plugin call a simple function which checks on any tag such as is_home, is_search, is_archive
3. Hook that function to either pre_get_posts, or parse_query, or similar
4. Activate WC and WC membership
5. Load home or any other page and see the error logged
6. Edit your Function to check if ( is_admin() || !$query->is_main_query() )
7. See the function still works but the error is gone.

The issue has to be fixed in Toolset as far I see it.
Please don't hesitate to correct me if wrong, but above POC pretty much shows that it is on "us" and not "them" ("us" because as said I had a plugin that run into the precise same issue when used tgt with their WC Membership, and there are several reports to other plugins in forums about this as well)
It may be unfortunate that WC Membership is doing what it is doing, but on the other hand it is on us to craft code as safe as possible.

Thanks.

#2004069

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Thanks for the extra info. Given how easy it is to reproduce this without Toolset I still think this is on the plugin authors—WordPress doesn't have an official action sequence but the fact that the documentation for conditional tags instructs you to use an appropriately timed hook to use them implies that there is an expected sequence—but it should be trivial to fix on our side.

I've put all the details into a ticket for the devs, and I'll escalate this and let you know when there is something to share.

#2071625

Hi Nigel, I just wanted to inquire if your Compatibility Team/Process was able to reach out to WooCommerce Memberships, or fix the issue in Toolset?

Can we help somehow?
I have an email conversation with WooCommerce Memberships support which I am of course willing to forward to you if it helps.
Unfortunately the client has no account with them, so I can't ask the client to do this "push" on their own.

Let me know if I can help so we can resolve this issue.

Cheers!

#2072205

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Beda

The internal ticket is on the current development board, so hopefully it will be worked on before we reach the end of this cycle.

It is straightforward enough to check if those functions exist or not before using them, the complication being that they need to exist so that the checks for an archive can work when using custom archive pages, for example, and by changing the normal hook sequence the plugin complicates that.

If the debug logs are being cluttered by these notices and you want to keep the logs on, you could suppress PHP notices in the meantime...

#2117505
Toolset-WPForms.png

Hey!

I get the same error in admin console when using plugins Toolset Blocks and WPForms Lite (see screenshort)!

When disable the plugin Toolset Blocks, the error disappears.

Can you please tell me what code should be placed in the file functions.php to get rid of these errors?

The code posted just above in this discussion doesn't help.

Thank you!

#2124953

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

@alex could you please report that separately, although the PHP notices are the same the circumstances are likely to be different, and the cause.

#2125149

Hi, Nigel!

The main circumstance is the following:
When disable the plugin Toolset Blocks, the ALL errors disappears.

You can install the plugin WPForms Lite yourself and see. DEBUG should be enabled.

If this is a problem of the WPForms plugin (more than 4 million installs), please indicate it. We will write to the technical support of this plugin, referring to your expert opinion.

But this is not what interests me now. The question is simple:
Can you please tell me what code should be placed in the file functions.php to get rid of these errors?

Thanks!

#2126779

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

@alex I've installed that plugin and done some debugging—the issue again happens because the pre_get_posts action gets fired much earlier than is customary (and before the main query is available). I've added details to the existing ticket, and asked the developers to look into it again.

#2127229

Hi, Nigel!
Thank you very much!
In standby...