Skip Navigation

[Resolved] search filter to check that two checkbox fields fields are checked

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

Problem:
How to combine two checkbox filter in to one checkbox and filter the results

Solution:
It can be achieved but it will require using the View's filter "wpv_filter_query" where you need to adjust the filters on the fly.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/search-filter-to-check-that-two-checkbox-fields-fields-are-checked/#post-1518499

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 4 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.

Our next available supporter will start replying to tickets in about 5.99 hours from now. 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 Chris 4 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#1517131

Hi,

I have a CPT that has several fields that are used for filters on a custom search.

There are 2 checkbox fields eg checkbox1 and checkbox2

I would like to include in the search filter just one search so a user would tick ONE box that would only list posts that have BOTH checkbox1 and checkbox2 checked.

Can this be easily acheived, I cant seem to work out how to do it.

Many thanks

#1517629

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

It can be achieved but it will require to use the View's filter "wpv_filter_query" where you need to adjust the filters on fly.

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

If you do not know how to do it - please send me a problem URL where you added the custom search 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.

#1518103
Screenshot 2020-02-20 at 09.41.08 1.png

PS - just to be crystal clear please see the attached image for what I am aiming to achieve from the user perspective, thanks

#1518121

Minesh
Supporter

Languages: English (English )

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

Unfortunately - the admin access details you shared with me is not working. Can you please send me working admin access details.

I have set the next reply to private which means only you and I have access to it.

#1518127

Sorry - Please try again with the same login info.

#1518443

Minesh
Supporter

Languages: English (English )

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

I would like to know, is it Ok for you if we change the "Custom Search Settings" to "Full page refresh when visitors click on the search button"? as using that method we can combine the two checkboxes as per your requirement.

#1518455

Yes that will be fine

#1518499

Minesh
Supporter

Languages: English (English )

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

So, I've added the following code to the 'Custom Code' section offered by Toolset:
=> hidden link

add_shortcode('set_field_checked', 'func_set_field_checked');
function func_set_field_checked($atts, $content){
    extract( shortcode_atts( array(
        'url_param'  => '',
        'value' => '',
    ), $atts ) );
    $res = '';
  
   if(isset($_REQUEST[$url_param]) && $value==$_REQUEST[$url_param]){
        $res = 'checked="checked"';
    }
    return $res;
}

add_filter( 'wpv_filter_query', 'func_combine_checkbox_filer', 10, 3 );
function func_combine_checkbox_filer( $view_args, $view_settings, $view_id )  {
   
  if ( in_array( $view_id, array( 123511 ) ) ) {
    
   			$target_field = "wpcf-access-vol";
    
              foreach ($view_args['meta_query'] as $key => $value):
                 
                    if ($value['key'] == $target_field) {
                        $required_index = $key;
                    }
              	endforeach;
            
    
   		 if ( isset($required_index) ) {
           	$view_args['meta_query'][] = array('key' => 'wpcf-access-fund-approved', 'value' =>1, 'type' => 'NUMERIC', 'compare' => '=' );
            }       
      }
 return $view_args;
}

Within your view, I've adjusted the checkbox filter at "Search and Pagination" as given under:
=> hidden link

<div class="form-group">  
	<div class="checkbox"><label for="wpv_control_checkbox_wpcf-access-vol">
      <input type="checkbox" id="wpv_control_checkbox_wpcf-access-vol" class="checkbox" name="wpv-wpcf-access-vol" value="Is AF Vol" [set_field_checked url_param="wpv-wpcf-access-vol" value="Is AF Vol"]>
      Offers Works Via the Access fund</label>
    </div>
</div>

Can you please check and confirm it works as expected:
=> hidden link

#1518685

My issue is resolved now. Thank you!