• Resolved helmsberger

    (@helmsberger)


    Dear WordPress community members,

    I’m really stumped on this one, have been at it for days…

    I have an advanced search function in my theme which works on the pre_get_posts, and it’s been working fine for a couple of years until recently.

    Now instead of displaying the results, have_posts shows as empty. I installed Query Monitor in my WordPress installation (and was blown away by the amount of queries for each page), and was able to locate in the list of queries being called the one which should be triggered by the advanced search.

    Query Monitor even put a purple “Main Query” indicator next to it, I copied the corresponding SQL SELECT code from the monitor, ran it directly in PhpMyAdmin and got back all the results that should be coming back for this search.

    My only conclusion is that the have_posts is not giving back results for the “Main Query” as indicated by the Monitor, although when I run is_main_query() it comes back as true.

    Where oh where oh where did I go wrong?

    Hopefully thankful,
    Helmsberger

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Some other code is corrupting the main query somehow. WP uses a number of global variables to run query loops. They tend to have unfortunate names which increase the chance of name collisions with plugin or theme code. Another possibility is another query and loop is run between the main query and your have_posts() where wp_reset_postdata() was not called afterwards. This would cause your have_posts() call reflect the state of this intervening query instead of the main query.

    You could try arbitrarily calling wp_reset_postdata() before have_posts(), but it’s much better to locate the true cause and fix that. If you were to disable all plugins, you should find that have_posts() works properly. If not, your theme is at fault. Restore the advanced search plugin and retest. If have_posts() now fails again, then advanced search is the problem. Otherwise, continue with each plugin until the problem plugin is identified. Take up the issue with that plugin’s developer.

    Thread Starter helmsberger

    (@helmsberger)

    Thank you bcworkz,
    I’ll look into deactivating the plugins one by and one and seeing if it helps.

    I am puzzled though why this would only effect the search, wouldn’t such a plugin coding conflict also intercept the Main Query on any other page?

    Moderator bcworkz

    (@bcworkz)

    It’s indeed a mystery. I am merely guessing that it’s an add-on code conflict because a fundamental WP process is not working. A WP bug or add-on code conflict somewhere are the only possible explanations. A WP bug like this would have been reported widely, thus… ??

    The conflict could be within code that begins if ( is_search()) : is one explanation.

    Thread Starter helmsberger

    (@helmsberger)

    Found the culprit plugin! (And I was quite surprised…)

    Thank you bcworkz. Beautiful community and forums _/|\_

    • This reply was modified 7 years, 7 months ago by helmsberger.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search not bringing back results of main query’ is closed to new replies.