Skip Navigation

[Resolved] Toolset Views exclude terms in filter + display posts with certain terms

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 1 voice.

Last updated by Minesh 1 month ago.

Assisted by: Minesh.

Author
Posts
#2846421
checkbox-filter.jpg
quebec-posts.jpg
custom-taxonomy-terms.jpg
toolset-view-checkbox-filter.jpg

Hi there,

We are using the Toolset Views plugin (legacy version) on the site for filtering posts.
We have a CPT called "Resources" and a custom taxonomy called 'Resource Categories', and we need to add a checkbox filter.

I have the checkbox filter (the terms in Resource Category) already added in to the 'Search and Pagination' box (screenshot attached toolset-view-checkbox-filter.jpg), and it's displaying correctly.
We also want to exclude certain terms in the checkbox filter, so we have used the php hook "wpv_filter_taxonomy_frontend_search_available_terms" to make that happen.

We would like to allow only one term and its child terms to be displayed in the checkbox filter and by using the code below, it worked:

Below is my code:

`
add_filter( 'wpv_filter_taxonomy_frontend_search_available_terms', 'allow_specific_terms_and_children', 10, 3 );

function allow_specific_terms_and_children( $terms, $taxonomy, $view_id ) {
if ( $view_id == 4538 && $taxonomy == 'resource-category' ) {

$allowed_parent_ids = array( 45); // IDs of parent terms you want to keep
$new_terms = array();

foreach ( $terms as $term ) {

if ( in_array( $term->term_id, $allowed_parent_ids ) || in_array( $term->parent, $allowed_parent_ids ) ) {

$new_terms[] = $term;
}
}
return $new_terms;
}

return $terms;
}

`
When the code is added in, it is displaying the 'Funding Sources' term and its child terms (screenshots attached: - 'custom-taxonomy-terms.jpg' + - 'checkbox-filter.jpg' ):

That part is working, and it's filtering correctly. However, instead of displaying all the posts on the right, we want to query/show posts that are tagged with the term 'Funding Sources' from the start too. We have used another php hook to make that work, please find the code below:

`
add_filter( 'wpv_filter_query', 'filter_view_by_terms', 10, 3 );
function filter_view_by_terms( $query_args, $view_settings, $view_id ) {
if ( $view_id == 4538 ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'resource-category', // Or your custom taxonomy
'field' => 'slug',
'terms' => array( 'funding-opportunities' ), // Slug of the tag
'operator' => 'IN',
),
);
}
return $query_args;
}

`

Adding that code snippet, it displays posts that are tagged with 'Funding Sources', but the filtering isn't working.
When I selected the term 'Quebec' on the filter, it's still showing all the posts. It should be showing 4 posts if 'Quebec' is selected (screenshot attached quebec-posts.jpg).

Below is a test page that we have created for testing.
hidden link

It seems adding the 'wpv_filter_query' code snippet caused the filter to stop working.

What is the issue?
What is the best way to display posts that are tagged with the term 'Funding Sources' from the start , and at the same time the checkbox filter should display the term 'Funding Sources' and its child terms (British Columbia , Manitoba, Nation-wide, Newfoundland, Quebec, etc..)?

thank you!

#2846523

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

What if you try to use the following code and check if that help you to resolve your issue:

add_filter( 'wpv_filter_query', 'filter_view_by_terms', 10, 3 );
function filter_view_by_terms( $query_args, $view_settings, $view_id ) {
if ( $view_id == 4538 ) {

                if(!isset($_REQUEST['wpv-resource-category[]'])){
                           $query_args['tax_query'] = array(
                                                        array(
                                                       'taxonomy' => 'resource-category',
                                                       'field' => 'slug',
                                                       'terms' => array( 'funding-opportunities')
                                                       'operator' => 'IN',
                                                      ));

                }
}
return $query_args;
}


Can you please try to use above code and check if that help you to resolve your issue.

#2846608

Thank you @Minesh ! When I added in your code snippet, it still didn't work.
It was still showing all the posts when I selected one of the provinces, like 'Quebec'.

I managed to solved the issue by using chatgpt. Below is their description of the issue + solution:

"When you hook into wpv_filter_query, you’re essentially taking over the query, and if you’re not careful, you wipe out the taxonomy filters that Views already built for you.

When you use:
add_filter('wpv_filter_query', 'my_custom_query', 10, 2);

You accidentally remove the taxonomy conditions that Views already added. So the taxonomy filter “stops working” because it’s no longer in the query.

The golden rule : Modify the existing query — don’t replace it.

If you must modify tax_query, sometimes you need to add a taxonomy condition. Do it like this:

add_filter('wpv_filter_query', 'extend_tax_query', 10, 2);
function extend_tax_query($query, $view_settings) {

if (!isset($query['tax_query'])) {
$query['tax_query'] = [];
}

$query['tax_query'][] = [
'taxonomy' => 'category',
'field' => 'slug',
'terms' => ['featured']
];

return $query;
}

This adds to the existing taxonomy filters instead of nuking them."

When I added their code snippet (I modified the taxonomy slug + term), and it's working now.

thank you!

#2846777

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok great - thanks for sharing. Please feel free to open a new ticket with every new question you may have.