Skip Navigation

[Resuelto] Filtering content by Checkboxes selection

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

Problem:
A select custom field is used as a Views filter but shown as checkboxes so that more than one option can be selected, but the View results do not work as expected.

Solution:
You need to modify the query arguments with the wpv_filter_query hook before the query is run.

Supporter provides sample code here (https://toolset.com/forums/topic/filtering-content-by-checkboxes-selection-2/#post-1101628) and the final version used by the client is shared here (https://toolset.com/forums/topic/filtering-content-by-checkboxes-selection-2/#post-1101696)

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

This support ticket is created hace 6 años, 2 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 7 respuestas, tiene 3 mensajes.

Última actualización por enma hace 6 años, 2 meses.

Asistido por: Nigel.

Autor
Mensajes
#1101436

I have a location field, which is populated with different sections of a city.
In content creation, this is a dropdown select, so in the back-end you can select only one location.

On the front-end display, I want a user to select the checkboxes that correspond to different locations.

I have created a filter for the location field, type checkboxes. But I can not get it to work. When I select a single element in the front-end it works, but when I select several it does not show any results.

I have been reviewing this answer, https://toolset.com/forums/topic/filtering-content-by-checkboxes-selection/, but in my case I would not like to use taxonomies since in the back-end you should only allow to select an area.

I have the latest version installed

Cheers

#1101617

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - do you mean you want to apply OR condition when you click on all your checkboxes field?

#1101622

Hello,
Yes I want to apply OR condition when I click on more than one checkbox.

#1101624

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Can I have problem URL where I can see your filters and access details?

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

#1101628

Nigel
Supporter

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

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

Hi there

This won't work unless you modify the query using the Views API.

If you turn on Views debugging (at Toolset > Settings > Front-end content) and examine the query arguments when you have checked more than one option, you will see they look something like this:

 [meta_query] => Array
        (
            [0] => Array
                (
                    [key] => wpcf-priority
                    [value] => 2,3
                    [type] => CHAR
                    [compare] => =
                )

            [relation] => AND
        )

That's from my test site, where I have a filter for a priority field.

The key is that I have selected two values, 2 and 3, but the compare argument of '=' expects only one.

So you need to use the wpv_filter_query API filter to modify the resulting query and change the compare argument to 'IN'.

Here is a simple example of that:

 function tssupp_mod_query( $view_args, $view_settings, $view_id ){

	if ( in_array( $view_id, array( 99 ) ) && isset( $view_args['meta_query'] ) ) { // Edit the View ID of 99 (or IDs)

		$view_args['meta_query'][0]['compare'] = 'IN';

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

This will work if you only have one custom field filter on the View, but if you have more than one you will need to edit that code to handle that possibility to make sure you are updating the compare argument for the right field.

#1101639

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Passing the ticket to Nigel.

#1101696

Hello Nigel,
I modify the query using the Views API and it works.

function tssupp_mod_query( $view_args, $view_settings, $view_id ){
 
    if ( in_array( $view_id, array( 316 ) ) && isset( $view_args['meta_query'] ) ) { 
		$arr_length = count($view_args['meta_query']);
		for($i=0;$i<$arr_length;$i++){
			if ($view_args['meta_query'][$i]['key'] = 'wpcf-distrito'){
				$view_args['meta_query'][$i]['compare'] = 'IN';
				break;
			}
		}        
    }
    return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_mod_query', 101, 3 );

But I have a problem when the parameter has white space, like these:

[meta_query] => Array
        (
            [0] => Array
                (
                    [key] => wpcf-distrito
                    [value] => Ciutat Vella
                    [type] => CHAR
                    [compare] => IN
                )

            [relation] => AND
        )
#1101711

Hi Nigel,
I solved the problem by passing the values as an array

$avalues = explode(',',$view_args['meta_query'][$i]['value']);
$view_args['meta_query'][$i]['value'] = $avalues;

Thanks for your help.