Saltar navegación

[Resuelto] Checkboxes filter is not working with AND

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

I want to have the checkboxes filter selection logic to be AND instead of the default OR.

Solution:

By default, the filter logic is OR. If you want to have AND you need to use the "wpv_filter_query". (It is considered as a custom coding and outside of the support scope)

Example:

add_filter( 'wpv_filter_query', 'we_custom_search_criteria', 101, 3 );

function we_custom_search_criteria( $query_args ,$view_settings ) {
  $view_id = 3142; // set view id (wpv_view_count)
  if ( isset( $view_settings['view_id'] ) && $view_settings['view_id'] == $view_id ) {
    foreach( (array)$query_args['meta_query'] as $k => $v ) :
      if( isset( $v['relation'] ) && $v['relation'] == 'OR' ) {
        $query_args['meta_query'][$k]['relation'] = 'AND';
      }
    endforeach;
  }
  return $query_args;
}

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

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

Este tema contiene 7 respuestas, tiene 2 mensajes.

Última actualización por shaneM-6 4 years ago.

Asistido por: Christopher Amirian.

Autor
Mensajes
#2404461
Screen Shot 2022-06-25 at 2.10.44 AM.png

Tell us what you are trying to do?
1. Go to enlace oculto
2. Select "Plant Nutrition" under "Filter by Product Type" drop down.
3. Check "Nitrogen (N)". See the result count is 207.
4. Check "Application: Foliar". See the result count is 255.

If I set "equal to" in query filter, it should be OR , right?
It seems like it will filter as AND. (If I select 2 checkbox, it should decrease the result count.)

I even tested "in", I got exactly same result.

Is there any documentation that you are following?
https://toolset.com/forums/topic/filter-with-custom-field-checkboxes-that-compare-using-or-instead-of-and-in-view/

Is there a similar example that we can see?
No

What is the link to your site?
enlace oculto

#2405299

Christopher Amirian
Colaborador

Idiomas: Inglés (English )

Hi there,

It seems to be more of a problem related to this issue:

https://toolset.com/errata/checkboxes-that-save-0-are-interpreted-as-checked-by-views/

Would you please follow the workaround and see if you get the correct results?

Thank you.

#2406081
Screen Shot 2022-06-27 at 10.10.05 PM.png

I have been set up like that already.

#2406949

Christopher Amirian
Colaborador

Idiomas: Inglés (English )

Ok thank you. I checked with my colleagues and the default behavior for the checkboxes filter between the items of the checkboxes is AND.

If you want to change it to OR then you need to use the wpv_filter_query Toolset hook. For more information:

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

There is a code sample that you can use and change the parameters according to your needs:

https://toolset.com/forums/topic/parametric-search-keeps-displaying-wrong-results/#post-357800

Thanks.

#2407179

So I was thinking opposite around. So

[ ✓ ] Option A
[ ✓ ] Option B
[ ] Option C

Currently, either Option A and Option B show up. (OR)
I want to filter result to be shown if both Option A and Option B is selected (AND).

But it seems like filter is using OR by default.

#2407533

Christopher Amirian
Colaborador

Idiomas: Inglés (English )

Hi there,

You are correct, I double-checked and it is OR. Sorry for the wrong answer.

From what I understand you still will need to use wpv_filter_query hook to change the default behavior.

Thank you.

#2407915

It seems like the structure of the query might changed from example you provided.
But I modified it and worked now.
Thanks you for your help and input!

add_filter( 'wpv_filter_query', 'we_custom_search_criteria', 101, 3 );

function we_custom_search_criteria( $query_args ,$view_settings ) {
  $view_id = 3142; // set view id (wpv_view_count)
  if ( isset( $view_settings['view_id'] ) && $view_settings['view_id'] == $view_id ) {
    foreach( (array)$query_args['meta_query'] as $k => $v ) :
      if( isset( $v['relation'] ) && $v['relation'] == 'OR' ) {
        $query_args['meta_query'][$k]['relation'] = 'AND';
      }
    endforeach;
  }
  return $query_args;
}
#2407917

My issue is resolved now. Thank you!