Skip Navigation

[Resolved] How to filter taxonomy select filter options for the taxonomy added as frontend filter with view

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

Problem:
How to filter taxonomy select filter options/terms for the taxonomy added as frontend filter with a view on multiple pages

Solution:
There is no native solution to filter the taxonomy filter options. The workaround would be to filter your taxonomy query with WordPress standard hook: pre_get_terms

You can find the proposed solution, in this case with the following reply:
=> https://toolset.com/forums/topic/exclude-pages-from-search-minesh-help-requested/#post-1622377

Relevant Documentation:

This support ticket is created 4 years, 7 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
- 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)

This topic contains 11 replies, has 2 voices.

Last updated by Pete 4 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#1621395
Plz see.png

Minesh kindly help overcome and issue on this ticket:
https://toolset.com/forums/topic/exclude-specific-pages-from-search/page/2/

Works great, not an issue.

However in another instance we have a similar issue on other pages. See this page:
hidden link

Click the area dropdown.

We would like, on this page and a number of others to exclude certain sub areas.

I tried using a Query Filter to select only the Main areas to show however, all the sub areas are also showing.

Is there a way round this or does the code you (Minesh) introduced need altering so it can be used more widely?

Thank you in advance. P

#1622377

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As you can see the code I shared:
https://toolset.com/forums/topic/exclude-specific-pages-from-search/page/2/#post-1611953

function func_specific_terms_viewfilter( $query ){
    global $post;
      
    if ( $post->ID == 12 && $query->query_vars['taxonomy'][0] == 'area' && !is_admin() ) {
          
        $meta_query_args = array(
                'relation' => 'AND', // Optional, defaults to "AND"
                                array(
                                'key'     => 'wpcf-display-on-home',
                                'value'   => '1',
                                'compare' => '='
                                )
                            );
    $meta_query = new WP_Meta_Query( $meta_query_args );
    $query->meta_query = $meta_query;
          
    }
}
add_action( 'pre_get_terms', 'func_specific_terms_viewfilter',101,1);

The above code is bind with the $post->ID == 12 .

So, the post ID of the page is 126.
=> hidden link

So, if you want to run the same code on multiple post IDs. You can change the following line of code from:

   if ( $post->ID == 12 && $query->query_vars['taxonomy'][0] == 'area' && !is_admin() ) {

To:

   $allowed_post_ids = array(12,126,999,222);
   if ( in_array($post->ID, $allowed_post_ids) && $query->query_vars['taxonomy'][0] == 'area' && !is_admin() ) {

Where:
- replace 999 and 222 with your additional post ids.

I hope the above solution will help you to resolve your issue.

#1622443

Hey Minesh,

Hope you well and keeping safe.

Ok, thanks for that. That 'seems' fairly logical.

I'm about to step out for a few hours however will give this a try and update the ticket later.

Thank you for sharing and explaining this 🙂

#1622833

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok great - no issues 🙂

#1623093

Hi there Minesh,

Thank you. This works pretty well however...

Check out this page: hidden link

It's post id is included in the code and initially works however in the area box, click and select an area, don't click SUBMIT.
Now click to change the area, everything is in the search...all the sub areas.

Why is this please?
I guess I am testing however is a visitor selects and area, does a search then changes their mind and changes the area, all the sub areas appear. This not ideal as we intend to add many more.

Thank you as always. Pete

#1623697

Minesh,

In addition to the above...adding more id's to this code slows the site down dreadfully.

I have these id's:
$allowed_post_ids = array(12, 126, 7842, 527, 7851, 7835, 7847, 7838, 7856, 26198, 26196, 13800, 7859);
if ( in_array($post->ID, $allowed_post_ids) && $query->query_vars['taxonomy'][0] == 'area' && !is_admin() ) {

If I leave all these id's, the backend of the site is dreadfully slow. If I remove them all, only leaving 1 the site is fine.

A little worrying that adding more id's make the site almost unusable? 🙁

#1623711

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The thing is that as you know I shared the workaround, there is no such feature and its custom code as well.

Its up to you if you want to go with a workaround or use the product as per the features provided. Unfortunately, I can share workaround and guide you but I do not have any control on its performance as its not native feature added to the product.

What if you try to change the if condition code as follows:

$allowed_post_ids = array(12, 126, 7842, 527, 7851, 7835, 7847, 7838, 7856, 26198, 26196, 13800, 7859);
if ( is_admin() ) { return $query; }
if ( $query->query_vars['taxonomy'][0] == 'area' &&  in_array($post->ID, $allowed_post_ids)  ) {

Does that make any difference?

#1624217
plx see.png

Hi there Minesh,

Sure I know this is a custom workaround and I'm grateful for your time and inventiveness in suggestions 🙂

Ok, I'm trying a different way of doing this. I've added a new taxonomy just for sub categories, so these will not even appear in the main nav.

So having created a backup I have deactivated your custom code to test what I need.

However, what else have you added to keep this column Display on Home?

I need to remove this too and then work with the principle nothing else has been added.

Many thanks again, Pete

#1624227

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please go to your screen options on that page at top right corner and uncheck the field checkbox.

if you are not using that custom field, then you should simply go to Toolset => Custom fields => Term fields and unassign the custom field group from your taxonomy.

#1625441

Hey Minesh,

Hope you are well.

Ok taking a different approach has certainly made things easier, however many thanks for your inventive code and solution 🙂

Can I ask, is there 'any' code elsewhere ref what you did originally?

We have done:
Can you please go to your screen options on that page at top right corner and uncheck the field checkbox.

However what was making this an option in the first place?

I have deleted your custom code in Toolset > Settings. I just want to be sure there is nothing else hidden away.

Many thanks.

#1625445

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As I shared with my previous reply:
if you are not using that "Display on Home?" custom term field, then you should simply go to Toolset => Custom fields => Term fields and unassign the custom field group from your taxonomy.

No - I do not add any other code as far as I know 🙂

#1625453

My issue is resolved now. Thank you!