Checkboxes AND logic

This topic contains 3 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 3 years, 7 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #20612
    tavy87
    tavy87
    Participant

    Hello,

    I’m using a custom field that displays as checkboxes. Relative to the other fields, the checkbox is AND logic. But it seems relative to ITSELF it is OR. For example, If I check 2 options, the results include matches to EITHER option, as opposed to only matches that match BOTH options. Is there any way to resolve this?

    Thank you for the help!

    #20616
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    Yes, it is possible to change that. On the custom field filter editor screen, there is a checkbox logic option, which is set to ‘OR’ by default. Changing that to ‘AND’ should resolve the issue.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #20632
    tavy87
    tavy87
    Participant

    My bad, I realize I’m actually using a Dropdown with Multiselect set to true to simulate checkboxes. This is due to the fact the styling of checkboxes is cumbersome on mobile. Can the logic for multiselect dropdowns by changed to AND?

    Thank you!

    #20640
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    Well, there is no option there to change the logic, but with a custom code it might be possible. I will make sure to add a logic option for multi-select cases, it makes sense.

    So, to resolve this, let’s try a custom code. Add this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

    add_filter( 'asp_query_args', 'asp_and_logic_on_multiselect_cf', 10, 1 );
    function asp_and_logic_on_multiselect_cf($args) {
        foreach ( $args['post_meta_filter'] as &$filter ) {
            if ( is_array($filter['value']) ) {
                $filter['logic'] = 'AND';
            }
        }
        return $args;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.