I have this custom taxonomy (SPECIAL PRODUCT) and I want to show in a view all items that have any category (Books, Radios, TV) chosen under this taxonomy. But I need to allow client to add or remove those categories.
So I would need a dynamic filter. For some reason I can't figure out how to do this with query filters. With "Any of the following" or similar, I have to activate the categories manually. There's no "wildcard" option. Or is there, where?
Hi,
Thank you for contacting us and I'd be happy to assist.
To suggest the best way to achieve this, I'll need to see how this view is set up in the admin area.
Can you please share temporary admin login details along with a link to a page where this view can be seen?
Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.
regards,
Waqar
Hi,
Thank you for sharing these details.
To include a taxonomy filter in your view, that checks for all the available terms, you can remove the taxonomy filter added in the view's "Query Filter" section and instead use the "wpv_filter_query" filter to programmatically include it:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )
Example:
add_filter( 'wpv_filter_query', 'filter_for_taxonomy_custom_fn', 1000 , 3 );
function filter_for_taxonomy_custom_fn( $query_args, $view_settings ) {
// if specific view
if ( ( !is_admin() && isset($view_settings['view_id'] ) ) && ( $view_settings['view_id'] == 1234 ) )
{
// slug of the target taxonomy
$taxonomy_slug = "book-type";
// get the list of all the terms from that taxonomy
$taxonomies = get_terms( array(
'taxonomy' => $taxonomy_slug,
'hide_empty' => false
) );
// store those term IDs in an array
foreach( $taxonomies as $taxonomy ) {
$terms[] = $taxonomy->term_id;
}
// if some term IDs are available, include a taxonomy filter in the view's query
if(!empty($terms)) {
$term_array[] = array(
'taxonomy' => $taxonomy_slug,
'field' => 'id',
'terms' => $terms,
'operator' => 'IN',
'include_children' => 1,
);
$query_args['tax_query'] = $term_array;
$query_args['tax_query']['relation'] = 'AND';
}
}
return $query_args;
}
Notes:
1. The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.
2. You'll replace "1234" with the actual View's ID and the "book-type" with the actual taxonomy's slug.
I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar