Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Last active December 19, 2023 19:11
Show Gist options
  • Save Crocoblock/f51c2552a2ef3bb676177445dd872d2c to your computer and use it in GitHub Desktop.
Save Crocoblock/f51c2552a2ef3bb676177445dd872d2c to your computer and use it in GitHub Desktop.
JetSmartFilters Filter by year (for checkbox/select/radio filters compatibility)
<?php
add_filter( 'jet-smart-filters/query/final-query', function( $query ) {
if ( ! isset( $query['meta_query'] ) ) {
return $query;
}
foreach ( $query['meta_query'] as $index => $meta_query_item ) {
if ( false !== strpos( $meta_query_item['key'], 'by_years__' ) ) {
$args = explode( '__', $meta_query_item['key'] );
unset( $query['meta_query'][$index] );
$column = $args[1] ?? '';
if ( empty( $column ) ) {
continue;
}
$value = $meta_query_item['value'];
if ( ! is_array( $value ) ) {
$value = array( $value );
}
$date_query = array();
foreach ( $value as $year ) {
$date_query[] = array(
'column' => $column,
'year' => $year,
);
}
$date_query['relation'] = 'OR';
$query['date_query'] = $date_query;
}
}
return $query;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment