Skip Navigation

[Resuelto] Single line custom field filter using checkbox not working properly

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

Problem:
How to change a filter for a checkboxes custom field to compare the checked options using OR instead of the default AND?

Solution:
Use the wpv-filter_query filter to modify the resulting meta query, specifically changing the relation to 'OR'.

An example of the code required would look something like this:

add_filter( 'wpv_filter_query', 'tssupp_change_checkboxes_filter', 101, 3 );
function tssupp_change_checkboxes_filter( $query_args, $settings, $view_id ) {
 
    if ( $view_id == 597 && isset( $query_args['meta_query'] ) ) { 
        $query_args['meta_query']['relation'] = 'OR';
    }
    return $query_args;
}

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

This support ticket is created hace 6 años, 10 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Etiquetado: ,

Este tema contiene 10 respuestas, tiene 2 mensajes.

Última actualización por marcB-6 hace 6 años, 10 meses.

Asistido por: Nigel.

Autor
Mensajes
#611267
Screenshot_11.png
Screenshot_12.png

I am trying to: add Single line custom field filter using checkbox

Link to a page where the issue can be seen:enlace oculto click on filters

I expected to see: for example, if i click on "Amsterdam" then it will give perfect results, but when I click "Bussum" with "Amsterdam" selected it says no results found.It should give results from "Amsterdam" and "Bussum" as it is checkbox filter.

Thanks

#611306

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Screen Shot 2018-01-31 at 08.29.21.png

Hi Marc

These are individual checkbox fields inserted as filters, right?

By default they are inserted with an 'AND' relationship, i.e. posts must match ALL of the checked fields.

You'll need to change that to 'OR', so that the requirement is that posts match ANY of the checked fields.

See the screenshot.

If you can't see the Query Filter section in your View you'll need to expose it via the Screen Options tab at the very top of the page.

#611329
Screenshot_13.png

Hi Nigel,

I have only 1 filter set.see attached screenshot.

Thanks!

#611355

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Screen Shot 2018-01-31 at 10.23.36.png

OK, sorry, it's a checkboxes field, not several checkbox fields, yes?

Can you check the settings for the field in Toolset > Post Fields.

What do you have for the save option?

#611387
Screenshot_2.png

OK, sorry, it's a checkboxes field, not several checkbox fields, yes?
Ans : Yes
See attached screenshot

#611398

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Hi Marc

Sorry, I misunderstood how you have this set up.

You have a single line text field, which you are displaying as checkboxes in the filter.

The resulting query uses and AND comparison when multiple values are checked, and in this case there is no option to change that.

To make this an OR comparison you would need to use the wpv_filter_query filter and hook into this View and manually override the relation argument of the meta_query to "OR".

If you don't want to add code to your site, make a field type that is a checkboxes field (or use a taxonomy which makes for more efficient queries when used as filters in any case) and then it will function as required.

#611403

I can not change field type.

To make this an OR comparison you would need to use the wpv_filter_query filter and hook into this View and manually override the relation argument of the meta_query to "OR".
Comment : Will it work for the view I have shown you? As it is single line field.

#611405

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Yes.

If you turn on debug mode for Views (Toolset > Settings > Front-end Content) and reload the page with two of the options selected you will see in the wpv_filter_query section the meta_query used by Views to retrieve the posts, and the relation will be set to AND.

Use wpv_filter_query to change that to OR.

#611637
Screenshot_12.png
Screenshot_13.png
Screenshot_14.png

Hi Nigel,

I tried with meta_query "OR" relation using below code. But it was not working properly then I played with the filter settings itself and fount solution.

add_filter( 'wpv_filter_query', 'prefix_change_vacature_filter', 99, 3 );
function prefix_change_vacature_filter( $query_args, $settings, $view_id ) {
    //global $current_user;
    if($view_id == 597){ 
        $query_args = array(
        'post_type' => 'vacature',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'meta_query' => array(
        'relation' => 'OR'
	),
      );
    }
    return $query_args;
}

I am attaching screenshot for the filter setting. It would help others 🙂

Thanks for your support.

#611691

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Hi Marc

I'll admit to being puzzled as to why what you have done works, but I'm not surprised your code didn't work, it doesn't do what's required.

If expected it to look more like this:

add_filter( 'wpv_filter_query', 'prefix_change_vacature_filter', 101, 3 );
function prefix_change_vacature_filter( $query_args, $settings, $view_id ) {

    if ( $view_id == 597 && isset( $query_args['meta_query'] ) ) { 
        $query_args['meta_query']['relation'] = 'OR';
    }
    return $query_args;
}

Anyway, thanks for sharing your findings with us.

#611706

Thanks Nigel!!