What you are calling “filter” is just a HTML select
element. You need the put the select
element in a form and filter the posts based on the selected option of this select
element.
For example, for the frontend:
The form:
<form method="post" action="<?php echo get_post_type_archive_link('rlt_rule');?>">
<?php
$filter_taxonomy = 'rlt_rule_headline';
wp_dropdown_categories(
array(
'show_option_all' => 'Show all headlines',
'taxonomy' => $filter_taxonomy,
'name' => $filter_taxonomy,
'orderby' => 'name',
'selected' => ( isset( $wp_query->query[$filter_taxonomy] ) ? $wp_query->query[$filter_taxonomy] : '' ),
'hierarchical' => true,
'depth' => 3,
'show_count' => false,
'hide_empty' => true,
)
);
<button type="submit">Search</button>
</form>
The filter process:
add_action( 'pre_get_posts', 'my_pre_get_post' );
function my_pre_get_post($query){
//limit to main query and frontend are. Add you own conditionals
if($query->is_main_query() && !is_admin()) {
$tax_query = array();
$tax_input = isset($_POST['rlt_rule_headline']) ? $_POST['rlt_rule_headline'] : '';
if(!empty($tax_input)){
$tax_query[] = array(
'taxonomy' => 'rlt_rule_headline',
'field' => 'id',
'terms' => $tax_input,
'operator' => 'IN',
'include_children' => true
);
}
}
$query->set('tax_query',$tax_query);
}
For the backend you to include your filter in the restrict_manage_posts
action hook as seen in this answer.