For further context, we have a global, all-results Algolia search (autocomplete) in our site’s header, which sits across all pages.
We have a secondary Algolia autocomplete which sits inside the Collections archive page, which only brings results back from the
collections post type – this is all working exactly as intended.
My question, however, relates to the standard WordPress search results page which appears when hitting ENTER inside the search input field (essentially
site.co.uk/?s=SEARCH_QUERY). It is this page that I only want to show results from the
collections post type when someone hits ENTER on the secondary Collections-only search input field and, when they search via the standard search input in the header, the normal operation of showing hits from all results should be displayed.
A couple things I’m noticing that may or may not help out here.
- In the
pre_get_postscallback that WP Search with Algolia uses, it checks for a
?post_type=thingparameter and if that’s present, it’ll only search with that post type. So, assuming that the form in question has a method of
get, then I think even just adding this to the form would change that behavior to match
<input type="hidden" name="post_type" value="collections" />
2. Alternatively, worth trying to add your own
pre_get_posts callback with a lower priority, like
11 and just manually setting the post type yourself. Only drawback to this one would be finding a way to determine which search form it came from, and globals like
$_REQUEST may be needed.
That said, I think the first option is sounding more promising.
Hi @tw2113, thanks for getting back to me so quickly!
I can confirm that your first suggestion worked perfectly – amazed it was such a simple, one-line fix, that’s saved me a massive potential headache.
Thanks for your help, happy to close this one off now.
It’s actually a trick that comes from WP core and how they can handle things. WP Search with Algolia codebase just happens to retain it with the code behind the scenes and for the requests to the Algolia API.