Skip Navigation

[Resolved] taxonomy checkbox filters problem

This support ticket is created 3 years, 1 month 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 3 voices.

Last updated by jozsefG 3 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#2229855
Untitled-1.jpg

Hello

I have a very strange problem with view filters. In case of the taxonomy checkboxes filters seems that selecting the optins don't narrow the search but widens it. Meaning that shows all playground with all selected equipment not just those that have all of the terms selected.

There also seems to be a bug, maybe it's related to my problem. I could not use the last inserted filter and it appears uneditable under Query Filter. I attach a screenshot with it. To be able to use the Features taxonomy I needed to add another filter so I just put Font types filter and hid it.

The site address is hidden link The view with the filter is right on the front page.
I use legacy views.
I also tried the jquery migration plugin but seems that it's not the jquery version that causes the problem.

Please assign me a supporter who's close to me in timezone. Mine is GMT+2. I need to solve this problem until tomorrow around noon...

#2229865

I copy here the code of the filters section too:

<div class="home-filters">
[wpv-filter-start hide="false"]
[wpv-filter-controls]
<div class="row">
	<div class="col-md-4">
        <div class="form-group">
            <label for="wpv-playground-type">[wpml-conditional lang="ro"]Tip teren de joacă[/wpml-conditional][wpml-conditional lang="hu"]Játszótér típusa[/wpml-conditional][wpml-conditional lang="en"]Playground Type[/wpml-conditional]</label>
            [wpv-control-post-taxonomy taxonomy="playground-type" type="select" default_label="Toate" url_param="wpv-playground-type"]
		</div>
  	</div>
	<div class="col-md-4">
		<div class="form-group">
            <label for="wpv-surrounding">[wpml-conditional lang="ro"]Împrejurimi[/wpml-conditional][wpml-conditional lang="hu"]Környezet[/wpml-conditional][wpml-conditional lang="en"]Surroundings[/wpml-conditional]</label>
            [wpv-control-post-taxonomy taxonomy="surrounding" type="select" default_label="Toate" url_param="wpv-surrounding"]
		</div>
  </div>
	<div class="col-md-4">
  		<div class="form-group">
            <label for="wpv-size">[wpml-conditional lang="ro"]Mărime[/wpml-conditional][wpml-conditional lang="hu"]Mérete[/wpml-conditional][wpml-conditional lang="en"]Size[/wpml-conditional]</label>
            [wpv-control-post-taxonomy taxonomy="size" type="select" default_label="Toate" url_param="wpv-size"]
		</div>
  	</div>
</div>
<div class="row">
	<div class="col-md-6">
        <div class="form-group">
            <label for="wpv-equipment" class="filter-title">[wpml-conditional lang="ro"]Echipamente[/wpml-conditional][wpml-conditional lang="hu"]Felszerelés[/wpml-conditional][wpml-conditional lang="en"]Equipments[/wpml-conditional]</label>
            [wpv-control-post-taxonomy taxonomy="equipment" type="checkboxes" orderby="id" url_param="wpv-equipment"]
        </div>
  	</div>
	<div class="col-md-6">
        <div class="form-group">
            <label for="wpv-feature" class="filter-title">[wpml-conditional lang="ro"]Caracteristici[/wpml-conditional][wpml-conditional lang="hu"]Tulajdonságok[/wpml-conditional][wpml-conditional lang="en"]Features[/wpml-conditional]</label>
            [wpv-control-post-taxonomy taxonomy="feature" type="checkboxes" orderby="id" url_param="wpv-feature"]
        </div>
  	</div>
</div>
  <div class="form-group" style="display:none;">
	<label for="wpv-elementor_font_type">[wpml-string context="wpv-views"]Font Types[/wpml-string]</label>
	[wpv-control-post-taxonomy taxonomy="elementor_font_type" type="select" url_param="wpv-elementor_font_type"]
</div>
  [wpv-filter-submit output="bootstrap" class="btn-secondary"]
[/wpv-filter-controls]
[wpv-filter-end]
</div>
#2229911

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The checkboxes filter is applied with an OR comparison, but you want it to work with an AND comparison.

It is not possible to do that via the settings of the View, you must use the API (the wpv_filter_query hook) to modify the query to use AND.

You can find an example of such code here: hidden link

(Note the comments where you need to make edits for your site.)

#2230127

Thanks Nigel for your quick response!
I added the code in functions.php, I changed the slug for one of the taxonomies: equipment and I changed the view ID.

function tssupp_mod_checkboxes_query($view_args, $view_settings, $view_id)
{
    $slug = 'equipment'; // Edit slug
    $metakey = 'wpcf-'.$slug;

    if (in_array($view_id, array( 1633 ))) { // Edit View ID(s)

        $meta_query_keys = array_keys($view_args['meta_query']);
        unset($meta_query_keys['relation']);
        foreach ($meta_query_keys as $meta_query_key){
            if ( is_array( $view_args['meta_query'][$meta_query_key] ) && isset($view_args['meta_query'][$meta_query_key][0]['key']) && $view_args['meta_query'][$meta_query_key][0]['key'] == $metakey ){
                $view_args['meta_query'][$meta_query_key]['relation'] = 'AND';
            }
        }
    }

    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_mod_checkboxes_query', 101, 3);

It is not working and I get a bunch of php errors. Would you take a look please?

#2230515

Hello,

I am working in Timezone GMT+8, hope it is not a problem.

Since it is a custom PHP codes problem, please provide a test site with the same problem, also point out the problem page URL and view URL, and where I can edit your custom PHP codes, I need to test and debug it in a live website, private message box enabled, thanks

#2230563

Thanks for the details, I can login into your website, I assume we are talking about the "Echipamente" checkboxes, they are terms of taxonomy "Equipments", so your custom PHP codes won't work.

In your case, it does not needs custom codes, you just need to do this:
Edit the post view "Homepage view", in section "Query Filter", change the taxonomy filter to:
Equipments slug in all of those set by the URL parameter wpv-equipment

More help:
https://toolset.com/documentation/user-guides/views/filtering-views-by-taxonomy/
All of the following
Return posts that belong to all the selected taxonomy terms.

#2232101

It seem to work correctly, thank you very much for your help!