Learn how to create search results filtering rules based on current users' search query.
In this article, we will cover how to create search result filters based on the user's current search query.
Example: A user enters the search query Hoodies on Sale
. We want to create a filter that shows only On Sale products if the user's search query contains the word sale
.
Here are the search results before applying this filter:
And after applying it:
With this type of filter, you can create very complex search rules based on the current search text:
...and so on. The number of examples is limited only by your imagination. This is a powerful filtering rule.
Below, we will cover how to create such filtering rules for all search result types: products, taxonomies, and users.
Note: Filters based on the current search query are available starting from Advanced Woo Search PRO 3.14.
Let's start by creating a filter based on the current search query for product search results.
Example: Create a rule to show only On Sale products if the current query contains the word Sale
.
Here are the steps to create such a filter:
1. Go to the plugin settings page -> Search Results tab, and scroll down to the Filter Results section.
2. Click the Filter products search results button and create the following filters:
Current search text -> contains -> sale AND
Product is on sale -> equal to -> Yes
OR
Current search text -> not contains -> sale AND
Product -> equal to -> Any
3. Now, save all changes and check your search results. The new filter should be applied to all your product search results if the user's query contains the word sale
.
Next, let's create filters for taxonomy archive page results.
Example: If the current search query contains the word colors
, show all available values for the Color attribute.
To create this filter, follow these steps:
1. Go to the plugin settings page -> Search Results tab, and scroll down to the Filter Results section.
2. Click the Filter taxonomies archive pages results button and create the following filters:
Current search text -> contains -> colors AND
Term page taxonomy -> Product Color -> equal to -> Any
OR
Current search text -> not contains -> colors AND
3. On the same Search Results page, make sure that the search for attributes is enabled. To do this, find the Archive pages option, enable the Attributes field, and click on the gear icon next to it to enable attributes related to product colors.
4. Finally, save all changes and check the search results. If the search query contains the word color
, the taxonomy results will show only values related to the Color attribute.
Finally, let's create a filter for user archive page search results.
Example: If the user's search query contains the word vendor
, show only users with the role Shop Vendor.
To create this filter, follow these steps:
1. Go to the plugin settings page -> Search Results tab, and scroll down to the Filter Results section.
2. Click the Filter users archive pages search results button and create the following filters:
Current search text -> contains -> vendor AND
User role-> equal to -> Shop Vendor
OR
Current search text -> not contains -> vendor AND
3. Also, ensure that the search for users is enabled for your search form. To do this, on the same page, check the Archive pages option, and ensure that the Users field is enabled. Click the gear icon next to it and enable all user roles that you want to be included in the search.
4. Finally, save all the changes and check your search form. If the search query contains the word vendor
, the search results will show only users with the role Shop Vendor.