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
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
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
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?
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.
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.
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?
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.
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?