Skip Navigation

[Résolu] [Views] Show only posts from current category, not subcategories in taxonomy View

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 support ticket is created Il y a 6 années et 3 mois. 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.

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)

Marqué : ,

This topic contains 2 réponses, has 2 voix.

Last updated by hugoC-3 Il y a 6 années et 3 mois.

Assisted by: Christian Cox.

Auteur
Publications
#607663

I try to make a view of posts belonging to a certain category but the posts from the underlying subcategories are also listed. How can I limit my view to hide the posts in subcategories and only show the posts in the current category?

I checked https://toolset.com/forums/topic/showing-products-only-in-their-sub-categories/#post-500301 but this doesn't seem to work for me.

Example page: hidden link

#607710

There's not an easy way to do this from wp-admin, but you can 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 taxonomy View IDs where you want to apply this modification. 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.

#608871

Thanks, I can explore a bit further now.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.