Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Preset filters without using URL › Reply To: Preset filters without using URL
August 18, 2023 at 3:14 pm
#45117
Keymaster
Hi,
Sure! It is possible to do programmatically via the Frontend Filters API.
It highly depends on the exact scenario, but this is the starter code you are looking for:
add_filter('asp_pre_get_front_filters', 'asp_change_a_filter', 10, 2);
function asp_change_a_filter($filters, $type) {
if ($type == 'taxonomy') {
$selected = array(
// page id => selected category ID,
1 => 123,
2 => 12344
);
foreach ($filters as $k => &$filter) {
// Go through the filter items via a loop
foreach ($filter->get() as $kk => $item) {
// Should this be selected?
if ( isset($selected[get_the_ID()]) && $item->id == $selected[get_the_ID()] ) {
$filter->attr($kk, 'selected', true, true);
} else {
// Unselect the rest
$filter->attr($kk, 'selected', false, true);
}
}
}
}
return $filters;
}
Unfortunately I could not test this right now, but should be very close to a possible solution.