Skip Navigation

[Resolved] Unexpected result with checkboxes in custom search

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

This topic contains 7 replies, has 2 voices.

Last updated by David Gimenez 4 years, 3 months ago.

Assisted by: Nigel.

Author
Posts
#1741263

Hi there.

I have the following problem

I have a custom post type that have a several custom fields. One of them are checkboxes.

The custom search to show that custom post types is configured AJAX results update when visitors change any filter values.

When I click in one checkbox works fine and the results are ok. If I click 2 checkboxes that works wrong because the results are working with relation OR not AND.

How can I solve this?

Example:

Restaurants with wifi: 2 restaurants
Restaurants with terrace: 1 restaurant

Restaurants with wifi + terrace = 1 restaurant (AND) but custom search show 3 restaurants (OR).

How can i solve this?

Thankss in advanced

#1741869

Nigel
Supporter

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

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

Screenshot 2020-08-17 at 12.28.42.png

I guess you are doing this in Blocks?

When I look at doing this in classic Views I can see an option for this when inserting the filter (screenshot).

But the same options are missing from the View block.

I'm adding this to the development board for classic features which have not been fully migrated to blocks.

In the meantime, you could switch to creating the View in classic Views.

Alternatively, you could add a code snippet that uses the wpv_filter_query API hook and changes the comparison argument to AND.

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

#1742131
Captura de pantalla 2020-08-17 a las 16.21.56.png
Captura de pantalla 2020-08-17 a las 16.21.59.png

Hi Nigel and thanks for your answer.

I am working with views (old way) and I don't see this dropdown that you show in that screenshot.

Look this pictures...

Is there any way to enable that comparison dropdown?

#1742203

Nigel
Supporter

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

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

Sorry, my bad.

My screenshot comes from a taxonomy filter, not a custom field filter.

I think we'll need to go down the route of a code snippet to modify the query.

Let me check what's required on my own test site and I'll get back to you, probably in the morning.

#1742263

Hi Nigel.

Wait please. I found this: https://toolset.com/forums/topic/how-to-filter-by-more-than-one-checkbox/#post-631805

I'll check it.

If I need your help I'll post here again.

Thanks again!

#1742839

Nigel
Supporter

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

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

I had a quick look at that link, I wasn't sure about the solution, so you can try this if you didn't get that to work:

/**
 * Modify View query filters for checkboxes custom field filter
 * to compare with AND instead of default OR
 */

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

    if (in_array($view_id, array( 88 ))) { // 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);

You just need to edit the slug of your checkboxes field and the ID of the View.

#1756061

Thanks for your help Nigel.

#1756063

My issue is resolved now. Thank you!