I’d like the WP search form to search for one specific custom post type (CPT). Normally I’d put this peace of code inside the form: <input type="hidden" name="post_type" value="events"/>
But it seems the Roots search rewrite overwrites this hidden input, is that so? And if so; how can I disable it? I have two CPT’s: events and artists and I’d like to create two search forms which each search through one CPT.
Would be great if that was possible while maintaining the search rewrite (because it looks nice!). So when searching for ACDC, maybe the query could (still) look like domain.com/search/acdc.
The nice search functionality does not have a good solution for CPTs. Your most reliable option is to disable nice search in /lib/config.php by commenting out the add_theme_support('nice-search'); line.
If you want to try something more complex, you can replace the roots_nice_search_redirect function in /lib/cleanup.php with something like this (not tested well):
Disable nice-search doesn’t sound nice :-). I’m not that good at programming so I’ll give your code a shot and if it doesn’t work out than I’ll go for disabling the nice-search. But thanks anyway!
I think I’m missing something. I’ve put the original hidden field in the form from templates/searchform.php but when I search for an event of which I know it exists it doesn’t return any results. The search URL does become /search/eventname?t=events…
Is your CPT publicly searchable? To see if it is something wrong with the search functionality or your CPT, put post in the post_type hidden input and then test against searching just posts (vs. pages, posts and events results).
It still searches for everything, but the results are kind of weird. When I search for an event which I know it exists it doesn’t return results. When I search for a generic term it returns pages, posts, and results from events and also artists.
If I do, everything works as expected (with the roots form code from my previous post). So then it must be somewhere in the code you’ve posted? Weird because it looks pretty straightforward…
If I disable nice search and reset the roots_request_filter everything works as expected. When I enable nice search and set the roots_request_filter like you’ve suggested search doesn’t work anymore.
My custom post type is named ‘events’. Say I type the search query ‘rock’ and there are 3 events with ‘rock’ in the eventname (custom post title). What happens is that somewhere it tries to forward to a custom post type page from events. The URL I get when I search for ‘rock’ is domain.com/events/event-with-rock-name/?t=events and it returns the 404 page. So it looks like the rewrite fails somewhere and / or roots tries to forward to an existing page (I noticed that when typing domain.com/events/event-with-r for instance, it forwards to domain.com/events/event-with-rock-name. Or is this default WP behavior?)
In addition to my previous answer, if I change roots_nice_search_redirect to the code below (added $query) than the URL rewrite seems to work (becomes domain.com/search/?t=events&s=query), but it also returns a 404 so search is not working.
Most of us just disable “nice-search” for anything other than the default base search functionality – does everything work as intended if you do that? Unfortunately I don’t have time to write and test this for you.