Hi Nick
I'm not surprised you are struggling to manipulate the query if it relates to a checkboxes field. Deep in the mists of time checkboxes fields were implemented, and the why's are lost to us now, even though we can determine the how's by looking at the code, puzzling at the choices.
To be fair, I think the challenges arise from having an options field where all of the option values may be the same; when setting up a checkboxes field, while giving the options different labels ("Red", "Blue", "Green") users often give them the same values ("1", "1", and "1").
So if you look in wp_postmeta at how the checkboxes field values are stored (as serialised arrays), the option values are stored, but also generated unique option keys, given that the values might not be the same.
So, on my local test site, a post with a checkboxes field "checkers" that has colours for options is stored like this:
a:2:{s:64:"wpcf-fields-checkboxes-option-39f8503ff967e1c6e89b40e1ebdf90f4-1";a:1:{i:0;s:3:"red";}s:64:"wpcf-fields-checkboxes-option-49bb77867db3c504ff31fb02e40e0600-1";a:1:{i:0;s:6:"purple";}}
Unserialised, that translates to
Array
(
[wpcf-fields-checkboxes-option-39f8503ff967e1c6e89b40e1ebdf90f4-1] => Array
(
[0] => red
)
[wpcf-fields-checkboxes-option-49bb77867db3c504ff31fb02e40e0600-1] => Array
(
[0] => purple
)
)
Anticipating your task, I set up my colours checkboxes field such that the stored values are unique (colour 'slugs', rather than '1').
That way the raw postmeta value includes the name of what I'm targeting, making it simpler to modify a View query to specify checkbox options to include or exclude.
(If you set up your checkboxes field options to just store '1' as the saved value this becomes much more difficult, as there is no public API to retrieve those scary-looking option keys, and I strongly encourage you to modify the settings for your checkboxes field so that the saved value for the options use unique values. If you have to do that, you will have to re-save posts that use the checkboxes fields for the stored values to be updated to match the new settings.)
To illustrate how you can then modify the View query using the wpv_filter_query hook, I adopted a simple scenario where I wanted to include any post with a checkbox option of "red" and exclude any post that has a checkbox option of "brown". (I'm not responding to the values set in the filters by users, just imposing those conditions by way of illustration.)
function tssupp_filter_query( $view_args, $view_settings, $view_id ) {
if ( in_array( $view_id, array( 24 ) ) ) {
$view_args['meta_query'] = [
'relation' => 'AND',
[
'relation' => 'AND',
[
'key' => 'wpcf-checkers',
'compare' => 'LIKE',
'value' => 'red',
'type' => 'CHAR'
],
[
'key' => 'wpcf-checkers',
'compare' => 'NOT LIKE',
'value' => 'brown',
'type' => 'CHAR'
],
]
];
}
return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_filter_query', 101, 3 );
This works because the query is just looking at the contents of the meta_value column in wp_postmeta as raw strings, and the way they are saved (as serialised arrays, which are strings) means we can search the text for matches for the option values we specify.
I think you should have enough there to modify your code?