Hi.
In a custom search, if I search for posts that belong to a term, also the posts will be found that belong to a child of that term,
Now if I look for posts that DON´T belong to a certain term, the posts belonging to a child-term of that term will be found nevertheless!
Is there a way to change that?
THANK YOU, regards, Achim
Hi, I have a custom code snippet you can use that may help.
Create a new View that includes a taxonomy term Query filter, set by a shortcode attribute. Insert the View using a shortcode, and add the shortcode attribute that contains the term you want to use as a filter. Example:
[wpv-view name="Your View Name" wpvcategory="parent-term-slug"]
The shortcode attribute name should match the attribute name in the View's Query filter. Next, add this to your child theme's functions.php file, or create a new code snippet in Toolset > Settings > Custom Code:
add_filter( 'wpv_filter_query', 'only_term_filter',99,3 );
function only_term_filter( $query_args, $views_settings, $view_id) {
$view_ids = array( 12345 );
if (in_array($view_id, $view_ids)){
foreach($query_args['tax_query'] as $tq) {
if( isset( $tq['taxonomy'] ) ){
$tax = $tq['taxonomy'];
$term = get_term_by('id', $tq['terms'], $tax);
$termChildren = isset( $term->term_id ) ? get_term_children($term->term_id, $tax) : null;
$query_args['tax_query'][] = array(
'taxonomy' => $tax,
'field' => 'id',
'terms' => $termChildren,
'operator' => 'NOT IN'
);
}
}
}
return $query_args;
}
Replace 12345 with the numeric ID of the new View. Now the View will show only posts that have the parent-term-slug assigned, not any of its child terms.