Frontend filters API
API for adding/modifying and removing front-end plugin filter boxes
Changing filter values within a filter box
add_filter('asp_pre_get_front_filters', 'asp_change_a_filter', 10, 2);
function asp_change_a_filter($filters, $type) {
foreach ($filters as $k => &$filter) {
// To check some of the attributes:
// $filter->label
// $filter->display_mode
// $filter->position
// $filter->id
// $filter->type() >> returns the filter type
// $filter->field() >> field that is filtered (if applicable)
// do a var_dump($filter->data) -> to see the filter data
// Go through the filter items via a loop
foreach ($filter->get() as $kk => $item) {
// Changing a filter attribute: label, selected, value, default by array ID
$filter->attr($kk, 'label', 'New Label', true);
// Remove the current item by array ID
$filter->remove($kk, true);
}
// You can also change/remove items by key, without a loop
// ..for a category/term filter, use the term ID
$filter->attr(123, 'label', 'New Label');
$filter->remove(123);
// ..for a custom field filter, use the field value
$filter->attr('value', 'label', 'New Label');
$filter->remove('value');
// Remove values from a custom field filter
if ( $filter->type() == 'custom_field' && $filter->field() == 'my_field' ) {
$filter->remove(array('value1', 'value2'));
}
// Adding a new value to a custom field filter
if ( $filter->type() == 'custom_field' && $filter->field() == 'my_field' ) {
$filter->add(array(
'label' => 'Value label 1',
'selected' => false,
'value' => 'value1'
));
// The second function argument allows setting the value position
// Adding a value after the 2nd option
$filter->add(array(
'label' => 'Value label 2',
'selected' => false,
'value' => 'value2'
), 2);
// Adding a value to before the last option
$filter->add(array(
'label' => 'Value label 3',
'selected' => false,
'value' => 'value3'
), -1);
}
}
return $filters;
}The WD_ASP_FrontFilters singleton class
Source
Usage
Methods list
create()
Parameters
Return values
add()
Parameters
Return values
set()
Parameteres
Return values
get()
Parameteres
Return values
remove()
Parameters
Return values
Examples
Adding custom taxonomy filter
Dropdown, checkbox and radio type custom field filters
Text and hidden type custom field filters
Date type custom field filter
Slider and Range slider custom field filter
Last updated