Skip Navigation

[Resolved] Split: Filter Single Taxonomy via Checkbox – add custom and/or toggle

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

Last updated by Minesh 4 months, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2708146

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.

#2708148

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

There is no such way to add custom custom toggle but I can help you with basic checkbox.

By default "AND" clause will be applied between multiple taxonomy terms.

With your view:
- hidden link

I've added the following custom filter for "And / OR" as checkbox:

<div class="form-group">
  <label for="wpv-custom-or">[wpml-string context="wpv-views"]AND (Default) / OR[/wpml-string]</label>
	<div class="form-check"><input type="checkbox" id="wpv_control_checkbox_wpcf-or" class="js-wpv-filter-trigger form-check-input" name="wpv-wpcf-or" value="OR"  [set_field_checked url_param="wpv-wpcf-or" value="OR"]  >
<label for="wpv_control_checkbox_wpcf-or" class="form-check-label">OR</label></div>
</div>

Where:
- As you can see the shortcode is added [set_field_checked url_param="wpv-wpcf-or" value="OR"] that will add the checked attribute when filter is checked.

I've added the following shortcode to "Custom Code" section with code snippet "toolset-custom-code" that will help us to make checkbox checked as we are using ajax :

add_shortcode('set_field_checked', 'func_set_field_checked');
function func_set_field_checked($atts, $content){
    extract( shortcode_atts( array(
        'url_param'  => '',
        'value' => '',
    ), $atts ) );
    
	$checked = '';
	
	 if(isset($_POST['search']['dps_general'])){
        foreach($_POST['search']['dps_general'] as $k=>$v):
            if($v['value'] == $value  and $v['name'] == $url_param){
                 $checked = 'checked="checked"';
            }
              
        endforeach;
   }
   
    return $checked;
}

I've modified the "wpv_query_filter" code as given under where I've adjusted the code for taxonomy relation "AND" and "OR" based on the checkbox selection:

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']['relation'] = 'AND';
		
		 if(isset($_POST['search']['dps_general'])){
					$checked = false;
					foreach($_POST['search']['dps_general'] as $k=>$v):
						if($v['value'] == 'OR'  and $v['name'] == 'wpv-wpcf-or'){
							 $checked = true;
						}
						  
					endforeach;
				if($checked) {
					$query_args['tax_query']['relation'] = 'OR';
				}
		}
	
        $query_args['tax_query'][] =array(
                'taxonomy' => 'category',
                'field'           => 'slug',
                'terms'        =>  $shortcode_attr_value
            );
          
          
    }
		
    return $query_args;
}

Can you please confirm it works as expected:
- hidden link

#2708151

Hi Minesh maybe I was not clear enough.

For AND as an example I would expect if
[x] Advisory & Consultancy
[] Data/Research
[x ] Resources/Tools

it to return only the three (3) posts that have Advisory & Consultancy AND Resources/Tools AND the shortcode value.

For OR as an example I would expect if
[x] Advisory & Consultancy
[x] Data/Research
[ ] Resources/Tools

it to return only the eight (8) posts that have Advisory & Consultancy OR Data/Research AND the shortcode value.

If I leave the OR box unchecked it behaves as I would it expect to for OR not AND. Maybe that helps.

#2708158

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

By default "OR" clause will be applied between multiple taxonomy terms.

With your view:
- hidden link

I've added the following custom filter for "And / OR" as checkbox:

<div class="form-group">
  <label for="wpv-custom-and">[wpml-string context="wpv-views"]OR (Default) / AND [/wpml-string]</label>
	<div class="form-check"><input type="checkbox" id="wpv_control_checkbox_wpcf-and" class="js-wpv-filter-trigger form-check-input" name="wpv-wpcf-and" value="AND"  [set_field_checked url_param="wpv-wpcf-and" value="AND"]><label for="wpv_control_checkbox_wpcf-and" class="form-check-label">AND</label></div>
</div>

Where:
- As you can see the shortcode is added [set_field_checked url_param="wpv-wpcf-and" value="AND"] that will add the checked attribute when filter is checked.

I've added the following shortcode to "Custom Code" section with code snippet "toolset-custom-code" that will help us to make checkbox checked as we are using ajax :

add_shortcode('set_field_checked', 'func_set_field_checked');
function func_set_field_checked($atts, $content){
    extract( shortcode_atts( array(
        'url_param'  => '',
        'value' => '',
    ), $atts ) );
    
	$checked = '';
	
	 if(isset($_POST['search']['dps_general'])){
        foreach($_POST['search']['dps_general'] as $k=>$v):
            if($v['value'] == $value  and $v['name'] == $url_param){
                 $checked = 'checked="checked"';
            }
              
        endforeach;
   }
   
    return $checked;
}

I've modified the "wpv_query_filter" code as given under where I've adjusted the code for taxonomy relation "AND" and "OR (default)" based on the checkbox selection:

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'];
		
		
		$and_checked = false;
		foreach($_POST['search']['dps_general'] as $k=>$v):
				if($v['value'] == 'AND'  and $v['name'] == 'wpv-wpcf-and'){
					 $and_checked = true;
				}
						  
		endforeach;
				
							
		 if(isset($_POST['search']['dps_general']) and $and_checked ){
					
			
			$tax_query = $query_args['tax_query'];
			$current_cat_args = array();
			foreach($tax_query as $k=>$v):
			
				
				if(isset($v['taxonomy']) and $v['taxonomy'] == 'category'){
					
						
						$current_cat_args = $tax_query[$k]['terms'];
						unset($query_args['tax_query'][$k]);
						
						$query_args['tax_query']['relation'] = 'AND';
						
						foreach($current_cat_args as $index=>$term_value):
								  $query_args['tax_query'][] = array('taxonomy' => 'category',
																	'field'    => 'id',
																	'terms'        => $term_value
															 );
										  
							endforeach;
						
						
				}
						  
			endforeach;
			
		}
		
        $query_args['tax_query'][] =array(
                'taxonomy' => 'category',
                'field'           => 'slug',
                'terms'        =>  $shortcode_attr_value
            );
          
          
    }
	
		
    return $query_args;
}

I hope this time you get the result as you wanted.

#2708163

This is awesome. Thank you. One finally request.

Would it be possible to instead of a checkbox for AND / OR to have a toggle / radio button.

UX wise it would be preferable.

#2708165

Minesh
Supporter

Languages: English (English )

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

That is on you actually but as I've quick solution for you I will help on that but:

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-split-filter-single-taxonomy-via-checkbox-make-custom-checkbox-filter-as-toggle-button/

You're welcome to mark resolve your ticket 🙂