Skip Navigation

[Resolved] Split: Setting Filter Results – advance filtering with OR clause for multiple taxonomy filters

This support ticket is created 2 years, 8 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 14 replies, has 2 voices.

Last updated by Pete 2 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2330665

Hi Minesh,

Goodness, I've wasted half a day because of a simple typo....very frustrating however thank you very much for this 🙂

Can I ask one more thing ref this View?
As you can see I have added a 3rd filter (Property Features)
hidden link

Now I would ideally like this to be Collections or Area, so visitors can refine their search.

However am I right in thinking, because Collections and Area are in the Taxonomy Filter of the View, this will never work?
I have tried this in the past, if I was to add say...Area instead of (Property Features) the View will break.

Thank you.

#2330669

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

As I understand, you want to apply OR clause between the two filters - right?
=> wpv-area="norfolk,suffolk,devon,cornwall" wpv-collection="sea-view"

So, that the view should display posts belongs to terms wpv-area="norfolk,suffolk,devon,cornwall" or wpv-collection="sea-views" - correct?

#2330709
Aa see plz.png

Hi Minesh, thank you.

Ok see the image on the page in question:
hidden link

I would like Property Features filter to be Area filter like this page:
hidden link

So if a visitor wishes to drill down to a different area they can.
However we cant add Area to the filter in the View > Search & Pagination.

Sure this isn't possible however worth an ask 🙂

#2330727

Minesh
Supporter

Languages: English (English )

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

As I understand, you mean that with the following page:
=> hidden link

You want to add Area as dropdown filter?

#2330739

Yes that's is correct, to remove Property Features and have Area dropdown on this page:
hidden link

#2330745

Minesh
Supporter

Languages: English (English )

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

I see the view is set to use shortcode attribute to filter with area and collection:

[wpv-view name="england-destination" wpvarea="norfolk,suffolk,devon,cornwall,south-coast,yorkshire,somerset,dorset" wpvcollection="sea-views" view_display="layout"]

Do you mean you want to keep those filters (area and collection) and still add a area as dropdown filter and remove property features dropdown filter?

#2330765

Yes that is correct.

Do you mean you want to keep those filters (area and collection) and still add a area as dropdown filter....yes!

and remove property features dropdown filter?...yes!

To change nothing in the set up, only to display area as a dropdown. Whenever we have tried this is breaks.

#2330885

Minesh
Supporter

Languages: English (English )

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

So, it required some additional filters and efforts.

I've removed the taxonomy query filters from "Query Filter" section of the following view and added the AREA as dropdown select filter.
=> hidden link

I've added the following code to "Custom Code" section offered by Toolset with code snippet "toolset custom code":
=> hidden link

The following filter is used to filter the view's query on fly and filter the view results with passed shortcode arguments for wpvarea and wpvcollection.

add_filter( 'wpv_filter_query', 'func_filter_multiple_shortcode_attributes', 10, 3 );
function func_filter_multiple_shortcode_attributes( $query, $view_settings, $views_id ) {
    if ( $views_id == 88377 ) {
        global $WP_Views;
     
      	$area_terms = $WP_Views->view_shortcode_attributes[0]['wpvarea'];
        $collection_terms = $WP_Views->view_shortcode_attributes[0]['wpvcollection'];
      
        $area_terms = explode(',', $area_terms);
        $collection_terms = explode(',', $collection_terms);
      
        if(defined('DOING_AJAX') && DOING_AJAX){
          
           $query['tax_query'][] = array(
            'taxonomy' => 'collection',
            'field'    => 'slug',
            'terms'    => $collection_terms,
            'operator' => 'IN',
            );
            $query['tax_query']['relation'] = 'AND';
         
        }else{
          
            $query['tax_query'][] = array(
            'taxonomy' => 'area',
            'field'    => 'slug',
            'terms'    => $area_terms,
            'operator' => 'IN',
            );
          
          $query['tax_query'][] = array(
            'taxonomy' => 'collection',
            'field'    => 'slug',
            'terms'    => $collection_terms,
            'operator' => 'IN',
            );
            $query['tax_query']['relation'] = 'AND';
        }
    }
    return $query;
}

The following "wpv_filter_taxonomy_frontend_search_get_terms_args" filter is used to restrict the Area dropdown options to the terms you passed to shortcode attribute: wpvarea="norfolk,suffolk,devon,cornwall,south-coast,yorkshire,somerset,dorset".
It will display only options as filter option for terms: norfolk,suffolk,devon,cornwall,south-coast,yorkshire,somerset,dorset

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'func_filter_tax_terms_dropdown', 10, 3 );
function func_filter_tax_terms_dropdown( $args, $tax, $view_id ){
     global $WP_Views;
    if ( $view_id == 88377  && $tax == 'area' ){
  		$area_terms = $WP_Views->view_shortcode_attributes[0]['wpvarea'];
     	$area_terms = explode(',', $area_terms);
        
        $args['slug'] = $area_terms;
         
    }
  
    return $args;
}

Can you please confirm it works as expected: hidden link

I hope this will satisfies your requirements 🙂

#2330919

My issue is resolved now. Thank you!

#2330963

Hi Minesh,

Sorry noticed an issue.

On this page: hidden link
If you click the pagination at the bottom, say to page 3, and then go 'back' to page 1...it then includes Scotland...this not right.

Same on this page: hidden link
However when you click pagination 2, Scotland is included again....it's not holding to just the areas in the shortcode.

Am I doing something wrong with this?

Thank you.

#2331381

Minesh
Supporter

Languages: English (English )

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

I've adjusted the "custom code" added as given under, can you please check if its working as expected now.

add_filter( 'wpv_filter_query', 'func_filter_multiple_shortcode_attributes', 10, 3 );
function func_filter_multiple_shortcode_attributes( $query, $view_settings, $views_id ) {
    if ( $views_id == 88377 ) {
        global $WP_Views;
     
      	$area_terms = $WP_Views->view_shortcode_attributes[0]['wpvarea'];
        $collection_terms = $WP_Views->view_shortcode_attributes[0]['wpvcollection'];
      
        $area_terms = explode(',', $area_terms);
        $collection_terms = explode(',', $collection_terms);
          
            $query['tax_query'][] = array(
            'taxonomy' => 'area',
            'field'    => 'slug',
            'terms'    => $area_terms,
            'operator' => 'IN',
            );
          
          $query['tax_query'][] = array(
            'taxonomy' => 'collection',
            'field'    => 'slug',
            'terms'    => $collection_terms,
            'operator' => 'IN',
            );
            $query['tax_query']['relation'] = 'AND';
        
    }
    return $query;
}

=> hidden link

#2331589

Thank you Minesh....thank you again.

You are a genius 🙂 No idea what your code dose however grateful it works and for your time.

Take care. Pete

PS, the system is not letting me mark this as Resolved!

#2331597

Minesh
Supporter

Languages: English (English )

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

Glad to know that solution I shared help you to resolve your issue.

You are welcome to mark resolve this ticket 🙂 Have a great day ahead!!

#2331601

Minesh
Supporter

Languages: English (English )

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

You can mark resolve ticket with different text like - Another issue is resolved and share your feedback 🙂

#2331603

Another issue is resolved, many thanks again 🙂