Skip Navigation

[Résolu] View filter to filter on child taxonomy terms

This support ticket is created Il y a 3 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Ce sujet contient 18 réponses, a 2 voix.

Dernière mise à jour par Jim Il y a 3 années et 3 mois.

Assisté par: Minesh.

Auteur
Publications
#2140595

Jim
ts1.png

I have a taxonomy with child taxonomies like this:
ASH 2019 (the top level term)
- Nieuwsbrief 1 (the child terms...)
- Nieuwsbrief 2
- Nieuwsbrief 3
- Nieuwsbrief 4

Now I made a view to display all posts belonging to a certain top level taxonomy term. In this case ASH 2019.
This displays the posts just fine.

But now I would like to add a dropdown selection to filter further down to only the child terms of the current top level taxonomy term.

Do you understand?

So, there are many more taxonomies with child terms but I only want to show the child terms of the current chosen taxonomy term.

Let me know if this is clear or if you need more info...

Thanks,
Jim

#2140977

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand, you want to display dependent dropdown that when you select top level term from dropdown X, another dropdown should be displayed dropdown Y that only shows the child terms belongs to selected term from dropdown X.

If this is correct there is no option or feature available to display dependent dropdown based on the parent and child terms.

You are welcome to file a new feature request using the following form:
=> https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

#2141001

Jim

Hi Minesh,
not exactly.

The page itself is like an archive page for a certain top level term.
Now all I want is to show a filter for the underlying terms (it's children so to speak)

So, in this case, I would like to have a dropdown with these options:
- Nieuwsbrief 1
- Nieuwsbrief 2
- Nieuwsbrief 3
- Nieuwsbrief 4

More clear now?

#2141005

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

I'm not sure where exactly you want to child term filter dropdown.

Can you please share problem URL and admin access details. Once I review your setup I'll be able to guide you in the right direction.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2141035

Jim

This is the view that needs the filter:
/wp-admin/admin.php?page=views-editor&view_id=92

Please let me know if you need more info.

Thanks!

#2141041

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

I've added the following filter to "Custom Code" section offered by Toolset:
- lien caché

function tssupp_restrict_terms( $terms, $taxonomies, $args, $term_query ){
 global $post;
   
   
    // 1. pages where form (or search View) is inserted
    $pages = array(93);
 
    // 2. which taxonomy (slug)
    $taxonomy = 'congres-categorie';
  
   $termID = $_GET['tid']; 
   $termchildren = get_term_children( $termID, $taxonomy );
  
  foreach ( $termchildren as $child ) {
    $found = get_term_by( 'id', $child, $taxonomy );
    $found_terms[] = $found->slug;
  }
  
    // 3. add terms to blacklist *OR* whitelist, not both
    $blacklist = array();
    $whitelist =$found_terms;
 
    if ( is_page( $pages ) && $taxonomies[0] == $taxonomy ) {
  
        if ( !empty( $blacklist ) ) {
 
            foreach( $terms as $key => $term ){
      
                if ( in_array( $term->slug, $blacklist ) ) {
                    unset($terms[$key]);
                }
            }
        } elseif ( !empty( $whitelist ) ) {
 
            foreach( $terms as $key => $term ){
      
                if ( !in_array( $term->slug, $whitelist ) ) {
                    unset($terms[$key]);
                }
            }
        }
    }
      
    return $terms;
}
add_filter( 'get_terms', 'tssupp_restrict_terms', 10, 4 );

On the following page I can see its displaying the only child terms now:
- lien caché

#2141867

Jim

Hi Minesh,
Thank you worked great.

But now not anymore... Not sure what happened. Looks like the query filter in the view got messed up. I cannot edit or delete teh filter either.
Can you take another look please?

#2141873

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Can you please check now: lien caché

Do you see it working as expected?

#2141903

Jim

No, it's not. The page is no longer filtering for the top level categorie (ash-2019 in this case). All top level categories are now displayed somehow...

#2141929

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

I can see now only child terms are displayed within the taxonomy filter dropdown. Do you have any other issue?

#2141947

Jim

Hi Minesh, yes the dropdown filter works.
But this page lien caché should only display ash-2019 posts
and now it shows all posts: lien caché

Any idea?

#2141951

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

I've adjusted the taxonomy filter url_param as given under:

<div class="form-group">
	<label for="wpv-congres-categorie">[wpml-string context="wpv-views"]Congres categorieën[/wpml-string]</label>
	[wpv-control-post-taxonomy taxonomy="congres-categorie" type="select" default_label="Alle" url_param="cucategory"]
</div>

I can see not its working as expected - can you please confirm it works at your end as well.

#2141967

Jim

Minesh, you are a genius! Thanks 🙂

One more thing...
The child terms show even when they are empty. Can it be made so that only child terms are shown when there are posts with that term?

And... the default term "Alle", should show all posts from the chosen top level category, but now when I set is to "Alle" it shows all posts from all top level categories...
Is this doable?

#2141987

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Can you please check now.

I've added the following view's filter "wpv_filter_query" to the "Custom Code" section offered by Toolset:

add_filter( 'wpv_filter_query', 'func_filter_posts_by_parent_term',99,3);
function func_filter_posts_by_parent_term($query_args, $view_settings, $view_id) {
    
    if($view_id==92){
       
      if(isset($_GET['wpv_view_count']) and $_GET['cucategory']==0){
        
        $query_args['tax_query'][] = array(
          								'taxonomy'=>'congres-categorie',
                                        'field'=>'id',
                                        'terms'=>array($_GET['tid']),
                                         'operator'=>'IN',
                                        'include_children'=>1
        							);
      }
      
    }
  return $query_args;
}

Can you please confirm it works as expected now.

#2142037

Jim

Wow, works like a charm.

But still one small problem:
when you select a child term, the page updates to the results and then the filter dropdown shows all taxonomy items again instead of just the chosen top level children.