Skip Navigation

[Resolved] Filtering categories in search options

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 18 replies, has 1 voice.

Last updated by francoC-2 1 week, 5 days ago.

Assisted by: Minesh.

Author
Posts
#2832897

We created a view with various search options. On the CATEGORIES options we need to shot only a selected set of categories and not all of them.
Is there a way to filter the categories to be selected? The "custom search field" does not allow to set a filter.

Please let me know if there is a workaround for this problem.
You can see the page at the following URL:
hidden link
We need to display in the left sidebar only the following categories that are sub-categories of "materiali e contributi" category:
- Articoli e pubblicazioni
- Eventi
- Nella stampa
- Newsletter
- Riconoscimenti
- Video

Thanks for your help

Franco

#2832902

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

To limit the terms or in other words to control the terms displayed with the custom search filter, you can additionally use the view's filter hook:

wpv_filter_taxonomy_frontend_search_get_terms_args

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_frontend_search_get_terms_args

For example, suppose you have a view with ID "999" and you have a search filter for taxonomy 'book-category' and you'd like to include only the child terms for the parent term name "history" which have term ID "1111". The code, in this case, would look like this:

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'func_allow_only_child_terms_in_custom_search_filter', 10, 3 );
function func_allow_only_child_terms_in_custom_search_filter( $args, $taxonomy, $view_id ) {

    // target parent term ID
    $term_id = 1111;
    
   // target taxonomy slug
    $taxonomy_slug = 'book-category';
   
   // target view's ID
    $target_view_ids = array( 9999 );

    // check if specific view
    if ( in_array( $view_id, $target_view_ids ) ){

        // get child terms of the target parent term
        $termchildren = get_term_children( $term_id, $taxonomy_slug );

        // include only those child terms in search filter
        $args['include'] = $termchildren;

    }
 
    return $args;
}

Where:
- Change 1111 with your original parent category id
- change 9999 with your original view id
- change 'book-category' with your original taxonomy/category slug

#2832907

thanks for the info, I need three more details:
1) whare shall I place the script? On the functions.php file of the child theme?
2) would you please re-write the script indicating an array of more than one child categories? the parent categfory ID is 3 and and some of the child categories slugs are: articoli-e-pubblicazioni, eventi, nella-stampa.

3) if I will have to create a different language for the website, shall I add another clone of this script with the ID and slugs of the new language categories?

Thanks.

Franco.

#2832908

Minesh
Supporter

Languages: English (English )

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

1) whare shall I place the script? On the functions.php file of the child theme?
=====>
You can add the script to "Custom Code" section offered by Toolset:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#benefits-of-adding-custom-code-using-toolset

2) would you please re-write the script indicating an array of more than one child categories? the parent categfory ID is 3 and and some of the child categories slugs are: articoli-e-pubblicazioni, eventi, nella-stampa.
===>
What you mean. The script is to fetch the all the child terms belongs to parent term ID "1111".

As per our support policy we entertain only one question per ticket. This will help other users seraching on the forum as well as help us to write correct problem resolution summery for the original issue reported.

Once you done with #1 and #2 I will split the ticket for the #3.

#2832909

Regarding point #2 is there a way to select only some of the children categories within the parent category? W don't need all subcategories but only some of them.

Thanks

#2832913

Minesh
Supporter

Languages: English (English )

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

Do you mean you have many parent and child terms with your custom search filter and you only want to control the specific parent child terms and display all other parent and child terms and the controlled child terms for specific parent term or you want to display only specific child terms belongs to specific parent only and no other terms?

#2832914

I need to display some of the child terms on ony one parent term as follows:

the parent category ID is 3
the child categories slugs are: articoli-e-pubblicazioni, eventi, nella-stampa.

#2832919

Minesh
Supporter

Languages: English (English )

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

So, the filter available on left sidebar will have only the following terms displayed - correct?

the parent category ID is 3
the child categories slugs are: articoli-e-pubblicazioni, eventi, nella-stampa.

#2832920

yes, correct

#2832922

Minesh
Supporter

Languages: English (English )

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

What if you try to use the following code:

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'func_allow_only_child_terms_in_custom_search_filter', 10, 3 );
function func_allow_only_child_terms_in_custom_search_filter( $args, $taxonomy, $view_id ) {
 
    // target parent term ID
    $term_id = 1111;
     
   // target taxonomy slug
    $target_taxonomy_slug = 'book-category';
    
   // target view's ID
    $target_view_ids = array( 9999 );
 
    // check if specific view
    if ( in_array( $view_id, $target_view_ids ) and $taxonomy==$target_taxonomy_slug ){
 
        // include only those child terms in search filter
        $args['slug'] = array('articoli-e-pubblicazioni', 'eventi', 'nella-stampa');
 
    }
  
    return $args;
}
#2832927

I still see in your code the following part:
// target taxonomy slug
$target_taxonomy_slug = 'book-category';
Is it still necessary if I then list the array of sub categories?

#2832936

Minesh
Supporter

Languages: English (English )

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

Yes - you can keep that as we are using that with if condition:

// check if specific view
    if ( in_array( $view_id, $target_view_ids ) and $taxonomy==$target_taxonomy_slug ){
#2832939

yes, but what is the slug that I need to replave with your example of "book-category" ? is this my parent taxonomy slug?

#2832943
Screenshot 2025-11-04 alle 14.23.55.png

I created the snippet but it doesn't work 🙁
see screenshot attached

#2833097

Minesh
Supporter

Languages: English (English )

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

You need to replace the "book-category" with your actual category/taxonomy slug.

Once you replace that and it does not work, please share admin access details with me.

I have set the next reply to private which means only you and I have access to it.