Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Created August 29, 2023 08:36
Show Gist options
  • Select an option

  • Save Crocoblock/d2ca4ca7fb8e22d1ab7e466a87a71337 to your computer and use it in GitHub Desktop.

Select an option

Save Crocoblock/d2ca4ca7fb8e22d1ab7e466a87a71337 to your computer and use it in GitHub Desktop.
JetSmartFilters Relation AND for checkbox filter (meta field)
<?php
add_filter( 'jet-smart-filters/query/final-query', function ($query) {
if ( ! isset( $query['meta_query'] ) || ! function_exists( 'jet_engine' ) ) {
return $query;
}
foreach ( $query['meta_query'] as $index => $meta_query_item ) {
if ( false !== strpos( $query['meta_query'][ $index ]['key'], 'checkbox_and__' ) ) {
$meta_key = explode( '__', $query['meta_query'][ $index ]['key'] )[1];
$query['meta_query'][ $index ]['key'] = $meta_key;
} else {
continue;
}
if ( ! is_array( $query['meta_query'][ $index ]['value'] ) ) {
$values = array( $query['meta_query'][ $index ]['value'] );
} else {
$values = $query['meta_query'][ $index ]['value'];
}
$regexp_query['relation'] = 'AND';
$i = 0;
foreach ( $values as $value ) {
$value = preg_quote( $value );
$regexp = '\:[\'\"]?' . $value . '[\'\"]?;s:4:"true"|\:[\'\"]?' . $value . '[\'\"]?;[^s]';
$regexp_query[ $i ] = array(
'key' => $meta_key,
'value' => $regexp,
'compare' => 'REGEXP',
);
$i = $i + 1;
}
$query['meta_query'][ $index ] = $regexp_query;
}
return $query;
}, 999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment