You can do this with basic input fields and the URL.
This is because example.com/?s=test
is a search URL for “test”, but example.com/category/foo/?s=test
also works, and searches the foo category.
Likewise, most of the other parameters WP_Query
takes also work, e.g. post_type
.
If we dig a bit deeper, all the pretty permalinks, and WP rewrite rules, are just regular expressions that convert pretty URLs, into the form index.php?s=test&cat=foo
etc.
So create a form, give it inputs with the names and values that match the keys and values of WP_Query
, and make sure it uses GET
as the method
so they appear in the URL.
For example, this search form searches a custom post type:
<form method="get" action="/">
<input type="hidden" name="post_type" value="mycpt" />
<input type="text" name="s" placeholder="search mycpts"/>
<input type="submit" value="search"/>
</form>
Notice that there is an input field with post_type
.
You can expand this with any of the standard HTML inputs, text boxes, select dropdowns, etc as long as the name
matches a value WP_Query
takes
A note however, WP search isn’t great, and the options for improving it are limited. You may also be tempted to search for posts via their meta values or add exclusions, resist this temptation! The performance cost of filtering/searching for posts by their post meta values is huge. It’s so huge they built a completely separate system of tables for doing it called taxonomies and terms.
For further reading, look at the official docs for WP_Query
https://developer.wordpress.org/reference/classes/wp_query/