Problem: I would like to create a View that shows posts that are associated to a specific term, but not its child terms.
Solution: Add the following custom code to your child theme's functions.php file:
add_filter( 'wpv_filter_query', 'only_term_filter',99,3 ); function only_term_filter( $query_args, $views_settings, $view_id) { $view_ids = [12345,67890]; if (in_array($view_id, $views_ids)){ foreach($query_args['tax_query'] as $tq) { if( isset( $tq['taxonomy'] ) ){ $tax = $tq['taxonomy']; $term = get_term_by('id', $tq['terms'][0], $tax); $termChildren = get_term_children($term->term_id, $tax); $query_args['tax_query'][] = array( 'taxonomy' => $tax, 'field' => 'id', 'terms' => $termChildren, 'operator' => 'NOT IN' ); } } } return $query_args; }
Replace 12345,67890 with a comma-separated list of View IDs where you want to apply this filter. Make sure your View's taxonomy filter is set up to respond to a shortcode attribute, something like "wpvcategory". Then pass the top-level term slug into the shortcode attribute like this:
[wpv-view name="your-view-slug" wpvcategory="parent-term"]
The View will return posts with the top-level term applied, but no posts that have to its child terms.
Relevant Documentation:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 2 replies, has 2 voices.
Last updated by 6 years, 10 months ago.
Assisted by: Christian Cox.