[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.
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.
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.
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:
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.