query - is_search called incorrectly

admin2025-01-07  3

I just noticed a new error message on one of my websites' pages that lists posts, / :

Notice: is_search was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.) in /home/wp_gmz28r/clubcorvettect/wp-includes/functions.php on line 4138

This site has been running properly for several months. I tried adding code that was suggested in this forum

dd_action('pre_get_posts', function( \WP_Query $q ) {
    if ( ! is_admin() // Only target the front-end
         && $q->is_main_query() // Target the main query
         && $q->is_search() // Target a search query
    ) {
        // do stuff
    }
})

but it didn't help. Any ideas?

I just noticed a new error message on one of my websites' pages that lists posts, http://clubcorvettect.com/category/events/ :

Notice: is_search was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.) in /home/wp_gmz28r/clubcorvettect.com/wp-includes/functions.php on line 4138

This site has been running properly for several months. I tried adding code that was suggested in this forum

dd_action('pre_get_posts', function( \WP_Query $q ) {
    if ( ! is_admin() // Only target the front-end
         && $q->is_main_query() // Target the main query
         && $q->is_search() // Target a search query
    ) {
        // do stuff
    }
})

but it didn't help. Any ideas?

Share Improve this question edited Jul 18, 2023 at 8:21 Rup 4,3904 gold badges28 silver badges29 bronze badges asked May 23, 2017 at 2:40 MikeMike 111 bronze badge 5
  • 1 Hi, Welcome to WPSE. Please add the code to your question, without knowing the code it's not really possible to help. – Johansson Commented May 23, 2017 at 3:29
  • Here's the code that I tried, but it had no effect: add_action('pre_get_posts', function( \WP_Query $q ) { if ( ! is_admin() // Only target the front-end && $q->is_main_query() // Target the main query && $q->is_search() // Target a search query ) { // do stuff } }); – Mike Commented May 24, 2017 at 3:55
  • You are trying to get the query before it's run. What are you trying to achieve? There is always an alternative. Add whatever you are trying to do to your question (using this edit link) and i will write a solution for you if possible. – Johansson Commented May 24, 2017 at 4:09
  • 1 I was trying to eliminate the error message and I saw that code was applied in a similar situation. Today, I disabled the WP plug-ins, one at a time, and found that disabling the custom 404 plug-in eliminated the error message. So I applied a simpler 404 page strategy. Problem solved. Thanks for your response, Jack. – Mike Commented May 25, 2017 at 5:00
  • 1 You are welcome. You can add your own answer and accept it, to help future visitors. – Johansson Commented May 25, 2017 at 16:28
Add a comment  | 

1 Answer 1

Reset to default 0

Hey Mike and welcome to the WordPress Stack Exchange!

First, it's good form to always include the code you use (or a link to where you found it) if you mention it in your post.

This error comes up when you use it prior to the search being run. The WordPress Codex doesn't have a ton of detail, but the basic gist is this: the function is very light weight and only returns a boolean. The reason it must be after the query is because it will always return false as it is checking wp_query() which doesn't exist until a query is run.

Unless your installation is aged and was recently updated, this error has probably been there for a while.

If the site is functioning the way you want, you can find the offending code and remove it since it's not in the right place anyways.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736255934a321.html

最新回复(0)