Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply post types from search forms to Instant Results as a filter #2510

Merged
merged 2 commits into from
Jan 18, 2022

Conversation

JakePT
Copy link
Contributor

@JakePT JakePT commented Dec 9, 2021

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 named post_type with the value product, 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:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Applicable Issues

Resolves #2508.

Changelog Entry

  • Adds support for search form post type fields to Instant Results.

@JakePT JakePT changed the title Apply post types from search form as filters. Apply post types from search form as filters to Instant Results Dec 9, 2021
@JakePT JakePT changed the title Apply post types from search form as filters to Instant Results Apply post types from search forms to Instant Results as a filter Dec 9, 2021
@JakePT JakePT self-assigned this Dec 9, 2021
@mckdemps mckdemps added this to the 4.0.0 (beta 2) milestone Dec 14, 2021
@mckdemps mckdemps requested a review from Rahmon January 4, 2022 18:19
@oscarssanchez oscarssanchez self-requested a review January 18, 2022 06:29
@mckdemps mckdemps merged commit dac55e0 into 4.x.x Jan 18, 2022
@mckdemps mckdemps deleted the feature/2508 branch January 18, 2022 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pre-select post types in Instant Results based on post_type inputs in the search form
3 participants