• Resolved Mark Howells-Mead

    (@markhowellsmead)


    I want to place a custom block I’ve developed which contains a React component for advanced search functionality using the REST API on the search results page. (Using the site editor.) How can I suppress the standard database calls which run when I call the search page with the ?s= parameter?

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

    (@bcworkz)

    You could unset the “s” query var through the “request” filter. The search term, if still needed, would need to be managed through some other mechanism. Then the problem becomes that WP will not know to serve your theme’s search template. If you use a classic theme, the template can be forced through “template_redirect” action. I’m not sure how to do the same with a block theme.

    You may be better off not using the “s” query var at all and implement your own search functionality independent from the default “s” query var.

    Thread Starter Mark Howells-Mead

    (@markhowellsmead)

    Ah, that’s a good tip, thank you. I’ve done some tests and you’re correct: unsetting $query_vars['s'] (or setting it to null) stops the search.html template from being loaded. If I set it to an empty string, then the search template is loaded, but the search query is still run. I’d still like to try and suppress the search query completely. Can you think of a way to achieve this through e.g. pre_get_posts or similar?

    Moderator bcworkz

    (@bcworkz)

    The momentum to make a query is already at full speed by the time “pre_get_posts” fires. I suppose you could use the “query” filter to set the SQL to null, which will cause $wpdb->query() to return false. It’ll prevent the query, but there may be undesirable knock-on effects. Wouldn’t hurt to try and see what happens.

    The practical problem will be in determining which SQL is an undesirable query and which should be allowed to proceed. You can minimize the different conditions to check for by adding this filter hook from the “request” callback so we know in general this is a search request. But there are likely other queries that still need to go through. I don’t think you can block all queries just because it’s a search request.

    If that doesn’t work for any reason, I recommend consulting the Query Overview description. It describes the entire process of handling a request and determining what sort of query to make based on it. You can then dig into the source code related to the processes described to see what opportunities there are to entirely suppress the default search query.

    Or just don’t use the “s” query var ??

    Thread Starter Mark Howells-Mead

    (@markhowellsmead)

    Thanks for taking the time to write such an extensive answer. I think I’ll probably go with unsetting $query_vars['s'] (so that the regular search is blocked) and potentially redirecting to a custom page with a custom query var.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Suppressing the search function on the search view’ is closed to new replies.