Skip Navigation

[Resolved] find posts that don´t belong to a SUB-term

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

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
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 1 reply, has 2 voices.

Last updated by Christian Cox 6 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1139109

a.R

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

#1139794

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.