Skip Navigation

[Resolved] Limit dropdown

This thread is resolved. Here is a description of the problem and solution.

Problem:

Limit the taxonomy terms dropdown to specific terms.

Solution:

This can actually be done by using the hook below.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_frontend_search_get_terms_args

This hook allows you to manually determine which items should be presented in the taxonomy dropdown.

This support ticket is created 2 years, 9 months 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 7 replies, has 2 voices.

Last updated by Pete 2 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#2122431
aa see here.jpg

Hi there,

Is there a way I can choose what is shown in the taxonomy dropdown?

On this page, one of many click property features:
hidden link

We need these for other reasons however there are way more than we need on the front end.

Ideally we would select about 8 and that would be it.

Is this possible out of interest?

Thank you.

#2122589

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pete,

Thank you for getting in touch.

We have a hook that should be able to help with this one.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_frontend_search_get_terms_args

This hook below is able to filter out the returned list and you can essentially tell the filter what terms are to be available on the frontend for filtering.

Thanks,
Shane

#2123359

Hey Shane,

Thank. Ok so I guess I would need something like this as suggested on the Toolset site:

// Following example limits the terms offered in the frontend filter to those which slug is either cat-one or cat-three.
add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'prefix_modify_get_terms_args', 10, 3 );

function prefix_modify_get_terms_args( $args, $taxonomy, $view_id ) {
$args['slug'] = array(
'cat-one',
'cat-three',
);
return $args;
}

But I guess just one of those?

And if so, where do I add this in the View?

Thank you.

#2123439

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pete,

In your case you will need to add this to your Toolset Custom code section at Toolset->Settings-> Custom Code and activate it.

// Following example limits the terms offered in the frontend filter to those which slug is either cat-one or cat-three.
add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'prefix_modify_get_terms_args', 10, 3 );

function prefix_modify_get_terms_args( $args, $taxonomy, $view_id ) {
if($view_id == 1234 && $taxonomy == 'tax_slug')
$args['slug'] = array(
'cat-one',
'cat-three',
);
return $args;
}

Now replace 1234 with the ID of your view, 'tax_slug' with the slug of the taxonomy that is displayed on the filter and in the array you will add all the terms that you want to be available for this filter.

This should then only give you those terms on the frontend filter.

Thanks,
Shane

#2124375
aa 2.jpg
aa 1.png

Hey Shane,

Yep we've used the custom code feature once or twice in the past.

Ok, see attachments. I have added the below code and added view ID and a couple of tax slugs and it's still not working:
hidden link

What is the 10,3 in this line of code?

'prefix_modify_get_terms_args', 10, 3 );

I feel I'm missing something here. Thank you. Pete

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'prefix_modify_get_terms_args', 10, 3 );

function prefix_modify_get_terms_args( $args, $taxonomy, $view_id ) {

if($view_id == 37131 && $taxonomy == 'tax_slug')

$args['slug'] = array(

'romantic-bolthole',

'coastal-village',

);

return $args;

}

#2125293

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pete,

I see one issue with how you've added the code, you didn't "tax_slug" to the slug of the taxonomy that the filter is displaying.

Trying changing this to the correct slug. If this doesn't work would you mind allowing me to have access to this setup on your staging environment to better adapt the code to your specific filter.

I've enable the private fields for your next response should this be needed.

Thanks,
Shane

#2125427

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pete,

Thank you, this should now be working.

The correct code is.

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'prefix_modify_get_terms_args', 10, 3 );



function prefix_modify_get_terms_args( $args, $taxonomy, $view_id ) {

if($view_id == 37131 && $taxonomy == 'property-feature'){

$args['slug'] = array(

'romantic-bolthole',
'coastal-village',
);
}
return $args;

}

The problem was that you didn't replace 'tax_slug' to 'property-feature'

Thanks,
Shane

#2126827

My issue is resolved now. Thank you!

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