Add epAutosuggestQueryFilter to allow filtering ES query based on input element #2909
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
Currently there is no easy way to filter an autosuggest query based on context. So in the case where you have a global autosuggest input on a page, there's no way to place a secondary autosuggest search on the same screen that covers, e.g., posts with a certain taxonomy term.
This follows the model of the existing filters,
epDataFilter
andepAutosuggestItemHTMLFilter
to allow the ElasticSearch query to be modified before it is sent. We check ifepAutosuggestQueryFilter
is defined, and if it is, the query is passed through that function. The function also receives the searchText and the input element, the latter of which is key to customizing the query, as we can identify an input by its attributes or even access its form atinput.form
.Closes #2908
Note: I didn't look into using
@wordpress/hooks
because my assumption is that package would bring in unwanted dependencies. If this option is pursued in the future, though, this functionality could be easily adapted. In that scenario I would recommend breaking each of these filters out into helper functions that can keep the current pattern for backward compatibility and fall back to the@wordpress/hooks
pattern if the global function isn't defined.How to test the Change
npm run build
ornpm run watch
Changelog Entry
Added - Autosuggest: filter the autosuggest ElasticSearch query by defining a
window.epAutosuggestQueryFilter()
function in JavaScript.Credits
Checklist: