This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Preset filters without using URL

#45117
Ernest MarcinkoErnest Marcinko
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.