When searching for posts with a parent term: When will posts with the child terms be found?
e.g.
FRUIT
- APPLE
- BANANA
When will posts with BANANA (but WITHOUT fruit!) be found when searching for FRUIT?
We believe to have seen various behaviour in that point.
THANK YOU, kind regards,
Achim
Hello and thank you for contacting the Toolset support.
By default, the view will look only for the posts with the searched/selected term. If you would like to search for posts with children terms you will need to alter the view's query arguments. This can be done by adding the include_children parameter to the tax query. Read more about it here https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
The custom code needs to hook to the wpv_filter_query hook.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Something like the following should work. You will need to use the view's ID on line 2 and the taxonomy slug on line 6
add_filter( 'wpv_filter_query', 'taxonomy_view_func', 10, 2 );
function taxonomy_view_func( $query, $setting ) {
if ( $setting['view_id'] = 43 ) {
if ( isset( $query['tax_query'] ) ) {
foreach ( $query['tax_query'] as $k => $v ) {
if(isset($v['taxonomy']) && $v['taxonomy'] == 'animal-group') {
$query['tax_query'][$k]['include_children'] = true;
break;
}
}
}
}
return $query;
}
I hope this helps. Let me know if you have any questions.
My issue is resolved now. Thank you!
Now I put this in my functions.php, and it gives me a FATAL ERROR:
add_filter( 'wpv_filter_query', 'taxonomy_view_func', 10, 2 );
function taxonomy_view_func( $query, $setting ) {
if ( $setting['view_id'] = 5522 ) {
if ( isset( $query['tax_query'] ) ) {
foreach ( $query['tax_query'] as $k => $v ) {
if(isset($v['taxonomy']) && $v['taxonomy'] == 'armethoden') {
$query['tax_query'][$k]['include_children'] = false;
break;
}
}
}
}
return $query;
}
Hello Achim, I reopened this ticket to check if you finally got the custom code working or not. If yes, please mark this ticket as resolved. If no, allow me access to WordPress and FTP access to check this further. Please note that I won't touch the custom code without having FTP access in order to not break the site. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
I will also need to know where the view is used, and a test scenario, so I don't have to guess:
- Select term xxx in the following filter.
- I expect to get results yyy, because they have the child term zzz.
So you don´t see any errors in my code?
No, I don't see any error that should trigger a Fatal Error.