Now that I've added content for another category, I found a but in that code...
It works well in the sense that if no category is specified (I.e. if the url doesn't have any parameters) it shows the default one that I want (Staff). BUT, the problem is that none of the other categories now appear in the drop-down... I.e. I just have the default "Show all" (that is blank for me and that I hide via jQuery, but if I remove my jQuery code it appears) and Staff (first element in the drop-down, which is also the default I set), but none of the other taxonomy's categories appear.
What I want it the Staff to be visible and selected by default, but the option to select something else in the drop-down (and filtered through AJAX to prevent reload).
Here's the current code:
add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
// skip if blocks edit screen
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return $query_args;
}
// process if specific view
if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 17255) ) {
$found_index = 'no';
$target_tax = "team-and-supporters-category";
$target_terms = array('0' => 631);
// check if filter for the target taxonomy exists
for ($i=0; $i < (count($query_args['tax_query']) - 1) ; $i++) {
if ( $query_args['tax_query'][$i]['taxonomy'] == $target_tax) {
$found_index = 'yes';
}
}
// if filter for the target taxonomy doesn't exist, add a custom one
if( $found_index == 'no') {
$query_args['tax_query'][] = array(
'taxonomy' => $target_tax,
'field' => 'id',
'terms' => $target_terms,
'operator' => 'IN',
'include_children' => 1
);
}
}
return $query_args;
}
I also noticed that if I had the other categories in the array:
$target_terms = array('0' => 631, '1' => 633, '2' => 632, '3' => 634, '4' => 635);
Then it does show all of them in the select... but also doesn't really filter, as it shows everything... So, bottom line, if I select just one, just that one is also in the drop-down, and if I select many, there no filter by default (I.e. same as Show All)... Since I only have 5 categories, I guess I could just create a select manually... but I would like to avoid that to make it dynamic in case the client adds or removes one category.
Thank you.