hooks – Utilising an existing page while using the “s” query parameter


I’m trying to load/utilise an existing page template (templates/results.php) while making use of the “s” (search) query parameter. For example: example.com/results?s=lorem

Currently, this results in a 404.

I’ve got a filter using template_includes, however it appears that the page has already been “decided” before that hook/filter (is_search(), is_page('results') are both false) before loading the template. I could just load the template files based on some other parameters, but then I’d have to set the title and other things away from the 404 page.

The pagename and s fields are recognised in the global $wp_query‘s raw query vars, so it’d just be a case of “deciding” what the query actually is before the template is hit.

Usecase for this being able to show a filtered set of posts for a custom post type “listings”. The Listings archive already shows listings, automatically filtered for “open” ones, and optionally filtered for its taxonomy and “s” for any searches. I’d have the results page do the same thing, being able to pick up the same optional query parameters as its archive counterpart.

How can I load a page, and utilise an existing page template while still using the in-built s query parameter?

This answer‘s solution 1 mentions replacing the default ‘s’ query var with a custom one, aiming to avoid that and hopefully filter/hook in before WordPress decides to search the results page for the ‘s’ string

Chris Heath 5 months 2021-06-23T20:52:59-05:00 0 Answers 0 views 0

Leave an answer