Automattic / jetpack

Security, performance, marketing, and design tools — Jetpack is made by WordPress experts to make WP sites safer and faster, and help you grow your traffic.
https://jetpack.com/
Other
1.59k stars 800 forks source link

Feature Request: Search with pre-selected filters #27934

Open RavanH opened 1 year ago

RavanH commented 1 year ago

Impacted plugin

Search

What

We wish to use search a field that will dynamically have a filter pre-selected when used, for example, from an archive page of a taxonomy that is used for filtering in Jetpack Search.

How

Not sure if I’m able to explain this clearly enough but imagine: a visitor lands on a category archive page and then decides to do a search from the (sidebar or header) search field on that page. It would be great if that search would be done with that specific category automatically selected in the Jetpack Search popup filter.

What we’re currently doing is providing custom buttons that open the Jetpack Search popup. For example a button “Search in Photography” with a link like ?s=&category=photography. Clicking such a button will successfully open the Jetpack Search popup with the Photography category pre-selected. The user can then start entering search terms and all results will be in the desired category.

It works but… it would be even better if the user could start with entering a search term (in the sidebar or header) without first having to open the pre-filtered search via our custom button.

Thanks for considering or sharing how this could be achieved in a better way that we’re currently doing 🙂

gibrown commented 1 year ago

Hmmm... so not positive whether this will work as is for you, but I think it may...

https://jetpack.com/support/search/frequently-asked-questions/#results-filter-category is a way to apply a filter to all searches. It passes some extra query parameters up to the JS overlay. So in your case you could add a filter like that only when the user is on an archive page (e.g. limit searches to within the category rather than the example which excludes them). The only downside there is that the end user won't be able to remove the filter interactively, though maybe that is what you want here.

There is also a mechanism documented on https://jetpack.com/support/search/customizing-jetpack-search/#add-overlay-link where you can add some properties to a link which will apply the correct filters. I think this mechanism could work if you have a search button that you could add it to. If that doesn't work, then maybe we can figure out how to make it work.

RavanH commented 1 year ago

Hi @gibrown we're currently doing something similar to https://jetpack.com/support/search/customizing-jetpack-search/#add-overlay-link but instead of the data filter attributes, we use query string parameters that correspond with the filters we set up in Jetpack Search. This seems to works the same as data attributes described there.

So basically this is the idea, except that these links/buttons are not dynamic. Meaning, it'd be great if such a search button (or search field) would dynamically take the current archive term (or date or user or...) and pass that to the data filter.

Maybe a dedicated "Dynamic Search" block (and matching shortcode and function for traditional themes) included in the Jetpack Search plugin?

gibrown commented 1 year ago

Ya, it seems like it would definitely need new blocks (or custom code) that we don't currently have unfortunately.

cc @jsnmoon in case it is easy.

jsnmoon commented 1 year ago

You're right on the money, Greg; we currently don't have this kind of functionality.

We'll likely need to modify the search input event handler to support this use case (e.g., checking for new data attributes on the input element). This hypothetical "Dynamic Search" block would need to instantiate the input element with the requisite data attributes.