Apply post types from search forms to Instant Results as a filter #2510
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of the Change
It's a common practice to create search forms only for particular post types by adding an
input[name="post_type"]
to the search form, often hidden. For example, the WooCommerce product search form has a hidden input namedpost_type
with the valueproduct
, so that only Products are searched. This PR updates Instant Results to automatically apply a post type filter for any post type fields in the search form.Benefits
Instant Results will respect any post type filters that have been applied to an existing search form. For example, when submitting the form in the WooCommerce Product Search block/widget or
get_product_search_form()
Instant Results will automatically apply a post type filter so that only Products are searched. If Instant Results is configured with a Post type facet then the filter can be changed or removed using the facet.Possible Drawbacks
If a form has a
post_type[]
field, to support searching multiple post types, then this will result in no results if the Match Type setting for Instant Results facets is set to "All", as posts cannot have multiple types. Sites with forms like this will need to use the "any" option. However, these types of forms are very rare.Better support for this might be possible in future if a UI is configured to support per-facet match types.
Verification Process
When a regular search form is submitted, Instant Results should function as normal and search all post types. If the Post type facet is in use then those results can be filtered by post type.
When a search form with an
input[name="post_type"]
field is submitted, the results should only come from post type passed as the value of this field. If a Post type facet is in use then this facet's selection should reflect the post type from the search form. If a Post type facet is not in use, then results should still be filtered by the form's post types, but clearing filters should not clear the post type filter.Checklist:
Applicable Issues
Resolves #2508.
Changelog Entry