Skip Navigation

[Resolved] Filter Single Taxonomy via Checkbox

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
- 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 8 replies, has 2 voices.

Last updated by Minesh 4 months, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2707936

Tell us what you are trying to do?

I already have a filter by short code in place for a taxonomy e.g. Car Type to show only posts with Car Type - SUV.

In addition I would then like to be able to filter via checkboxes - [ ] Large [ ] Mid Size [ ] Small

On checking e.g. Large it should refresh the view and show all Car Type that have SUV and Large

If Large and Mid Size were checked it should show all SUV with Large or Mid Size.

#2707942

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

In addition I would then like to be able to filter via checkboxes - [ ] Large [ ] Mid Size [ ] Small
===>
What taxonomy holds those values? Do you have another taxonomy that holds those Large, Mid Size and Small terms? If yes:

Then you should add the custom search filter to your view to filter by the taxonomy that holds the those Large, Mid Size and Small terms.

#2707943

Hi Minesh,

it is the same taxonomy.

#2707944

Minesh
Supporter

Languages: English (English )

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

Can you please share problem URL where you added the view with filter with shortcode attribute as well as admin access details.

*** 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.

#2707968

Minesh
Supporter

Languages: English (English )

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

So there is a short code filter in place for category: organizations which works fine.

Now in addition I would like 3 checkboxes to filter in addition by category [ ] Advisory & Consultancy | [ ] Data/Research | [ ] Resources/Tools.
====>
I would like to confirm here when user select any checkbox for instance Advisory & Consultancy - do you only want to display results with "Advisory & Consultancy" or you want to filter with
organization and Advisory & Consultancy.

#2707991

Hi Minesh,

thank you for asking.

It should continue to filter by Organization always. So organization and Advisory & Consultancy.

#2708110

Minesh
Supporter

Languages: English (English )

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

With your view:
- hidden link

I removed the existing query filter from "Query Filter" section that you added for "category" to filter with shortcode attribute "wpvcategory".

Within the "Search and Pagination" section I've added the following filter category filter:

<div class="form-group">
	<label for="wpv-category">[wpml-string context="wpv-views"]Categories[/wpml-string]</label>
	[wpv-control-post-taxonomy taxonomy="category" type="checkboxes" url_param="wpv-category"]
</div>

Now, To reduce the category terms to your desired terms:
=> Advisory & Consultancy, Data/Research, Resources/Tool

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

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'func_modify_tax_terms_args_custom_search', 10, 3 );
function func_modify_tax_terms_args_custom_search( $args, $taxonomy, $view_id ) {
 
  $target_views = array( 11727 ); // View with tax filter we wish to modify
 
  if ( in_array( $view_id, $target_views ) ){
	 
	$term_slugs = array('advisory-consultancy','data-research', 'resources-tools'); 
	$args['slug'] = $term_slugs;
	
  }
 
  return $args;
 
}

To hook in the shortcode argument value that holds the category "organization" I've added the following filter hook to "Custom Code" section offered by Toolset with code snippet "toolset-custom-code"

add_filter('wpv_filter_query', 'func_hookin_shortcode_attribute_value', 99, 3);
function func_hookin_shortcode_attribute_value ( $query_args, $view_settings, $view_id ) {
	global $WP_Views;
	
     if( $view_id == 11727) {
        
		$shortcode_attr_value = $WP_Views->view_shortcode_attributes[0]['wpvcategory'];
	
        $query_args['tax_query'][] =array(
                'taxonomy' => 'category',
                'field'           => 'slug',
                'terms'        =>  $shortcode_attr_value
            );
          
          
    }
	
    return $query_args;
}

Can you please confirm it works as expected now:
Frontend: hidden link

More info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_frontend_search_get_terms_args
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

#2708131

Minesh that is amazing. Also thank you for being so thorough in explaining what you did.

To put the cherry on top could a toggle be added to switch between. AND / OR filtering for the checkboxes.

New threads created by Minesh and linked to this one are listed below:

https://toolset.com/forums/topic/split-filter-single-taxonomy-via-checkbox-add-custom-and-or-toggle/

#2708147

Minesh
Supporter

Languages: English (English )

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

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

As per our support policy, we entertain only one question per ticket. This will help other users searching on the forum as well as help us to write correct problem resolution summery for the original issue reported.

May I kindly ask you to open a new ticket with every new question you may have in future. For now, I split the ticket and handle the new question for adding and/or toggle with the following split ticket.
- https://toolset.com/forums/topic/split-filter-single-taxonomy-via-checkbox-add-custom-and-or-toggle/

You're welcome to mark resolve your ticket 🙂