Skip Navigation

[Resolved] Filter view page without sub-taxonomy.

This thread is resolved. Here is a description of the problem and solution.

Problem:
The issue here is that the user wanted to remove their child terms from their taxonomy frontend filters in views.

Solution:

The can be done by using the hook below.

add_filter( 'wpv_filter_taxonomy_frontend_search_available_terms', 'prefix_modify_list_of_terms', 10, 3 );
  
function prefix_modify_list_of_terms( $terms, $taxonomy, $view_id ) {
   
    $curated_terms = array();
  if($view_id == 478 && $taxonomy =='category'){
   
    foreach ( $terms as $term ) {
        if (  $term->parent == 0 ) {
            $curated_terms[] = $term;
        }
    }
  }
        return $curated_terms;
 
}

In order to get this to work for you, you will need to change 478 to the ID of your view and 'category' to the slug of the taxonomy that you want to remove the child terms for.

Add the code to your Toolset custom codes in Toolset -> Settings -> Custom Code and ensure that you've activated it.

Relevant Documentation:

For more information on this hook you can take a look at the link below.

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_frontend_search_available_terms

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 2 replies, has 2 voices.

Last updated by LennyS7960 4 years, 1 month ago.

Assisted by: Shane.

Author
Posts
#1998241

Tell us what you are trying to do?
I have a taxonomy filter that has the main taxonomy elements and also sub elements. Is it possible to make a filter so that it would only show the main taxonomy.
For example I have:
- Category 1
- - Sub category 1-1
- - Sub category 1-2
- Category 2
- - Sub category 2-1
- - Sub category 2-2

But on the filter dropdown select it would only show:
- Category 1
- Category 2
Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#1998341

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Lenny,

Thank you for getting in touch.

You can actually achieve this with the hook below.

add_filter( 'wpv_filter_taxonomy_frontend_search_available_terms', 'prefix_modify_list_of_terms', 10, 3 );
 
function prefix_modify_list_of_terms( $terms, $taxonomy, $view_id ) {
  
    $curated_terms = array();
  if($view_id == 478 && $taxonomy =='category'){
  
    foreach ( $terms as $term ) {
        if (  $term->parent == 0 ) {
            $curated_terms[] = $term;
        }
    }
  }
        return $curated_terms;

}

The hook allows you to filter out the child terms from the taxonomy list. What you need to do is to change the 478 to the ID of the view as well as change the 'category' to the slug of the taxonomy that you want to filter the list for.

If you want to do it for multiple taxonomies on the same search then you will do it like this.

add_filter( 'wpv_filter_taxonomy_frontend_search_available_terms', 'prefix_modify_list_of_terms', 10, 3 );
 
function prefix_modify_list_of_terms( $terms, $taxonomy, $view_id ) {
  
    $curated_terms = array();
  if($view_id == 478 && ($taxonomy =='category' || $taxonomy =='product_cat')){
  
    foreach ( $terms as $term ) {
        if (  $term->parent == 0 ) {
            $curated_terms[] = $term;
        }
    }
  }
        return $curated_terms;

}

As you can see i've added the 'product_cat' slug as well to the if statement. Now this code can be added to the custom code settings in Toolset -> Settings -> Custom Code and activate it.

Please let me know if this helps.
Thanks,
Shane

#1998993

My issue is resolved now. Thank you!