Search Results - Filtering Options
Whenever the apposite configuration setting "Search Filters" is enabled, the front-end search results page will display a button to filter the rooms/listings after having selected some stay dates.
It is possible to extend the default filtering options by adding custom HTML code and server-side (PHP) validations of such filters. If you're looking for a way to hide or edit the default filters (categories, amenities, price range), then creating an override for the "Search" View is probably the best solution.
Here's how to declare a hook to install new custom filtering options.
do_action( 'vikbooking_display_search_results_filtering', array &$section )
Fires during the loading of the search results filtering options to display the available filters.
Description
Plugins can use this filter to define at runtime some custom filtering options to be displayed along with the native filters.
Parameters
- &$section
-
(array) The associative list of custom filtering options.
Example
The example below shows how to define a new custom section within the default search result filtering options.
/**
* Trigger action to let Vik Booking display new custom filtering options.
*
* @param array $factory The current filtering options associative list.
*/
add_action('vikbooking_display_search_results_filtering', function(&$sections)
{
$sections = (array) $sections;
$sections['Foo Bar'] = '<strong>My custom filter</strong> <input type="text" name="filters[my_custom_filter]" value="" />';
}); Once custom filters have been defined, it is time to take care of actually applying them. Here's where the apposite hook comes handy.
apply_filters( 'vikbooking_apply_search_results_filtering', array $roomResult, array $requestFilters )
Fires during the application of the search filtering options for every room-type/listing available.
Description
Plugins can use this filter to apply at runtime their own custom filters.
Parameters
- $roomResult
-
(string) Associative array of room/listing result information.
- $requestFilters
-
(array) Associative array containing all filters to apply, it's the container of the filters submitted.
This is how the hook will fire, and it's expected to return either true or false for respectively including or excluding the current room-type/listing from the search results according to the input filters.
Example
The example below shows how to apply a dummy filter, which is supposed to have been previously declared through the apposite hook.
/**
* Trigger action to let Vik Booking apply custom filtering options.
*
* @param array $roomResult The available room-type/listing associative result information.
* @param array $requestFilters Associative list of current input filters submitted.
*
* @return bool
*/
add_filter('vikbooking_apply_search_results_filtering', function($roomResult, $requestFilters)
{
if ($requestFilters['my_custom_filter'] ?? null) {
// text content is expected to be matched against the listing name with this custom filter
if (stripos($roomResult['name'], $requestFilters['my_custom_filter']) === false) {
// listing name does not match against the filter input value
// exclude the listing from the search results
return false;
}
}
// the listing can be displayed in the search results
return true;
});