Skip to content

Instantly share code, notes, and snippets.

@ihslimn
Created April 16, 2024 10:57
Show Gist options
  • Save ihslimn/dcbb502357a804314d0df9aa9ade0dd8 to your computer and use it in GitHub Desktop.
Save ihslimn/dcbb502357a804314d0df9aa9ade0dd8 to your computer and use it in GitHub Desktop.
JetSmartFilters Get range filter min/max from CCT
<?php
add_filter( 'jet-smart-filters/range/source-callbacks', function( $callbacks ) {
$callbacks['jet_smart_filters_cct_values'] = 'Get from CCT field';
return $callbacks;
} );
add_filter( 'jet-smart-filters/filter-instance/args', function( $args ) {
$key = $args['query_var'];
if ( 'range' == $args['query_var_suffix'] && false !== strpos( $key, 'cct_range::' ) ) {
$key_array = explode( '::', $key );
$args['query_var'] = end( $key_array );
}
return $args;
} );
function jet_smart_filters_cct_values( $args ) {
$key = ! empty( $args['key'] ) ? $args['key'] : false;
if ( ! $key ) {
return array();
}
if ( false === strpos( $key, 'cct_range::' ) ) {
return array();
}
global $wpdb;
$prefix = $wpdb->prefix;
$key_array = explode( '::', $key );
$type = $key_array[1];
$field = $key_array[2];
$table = $prefix . 'jet_cct_' . $type;
$sql = "SELECT min( FLOOR( $field ) ) AS min, max( CEILING( $field ) ) AS max FROM $table";
$data = $wpdb->get_results( $sql, ARRAY_A );
if ( ! empty( $data ) ) {
return $data[0];
} else {
return array();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment