Skip to content

Instantly share code, notes, and snippets.

@JuliaKiselyova
Last active January 9, 2024 10:13
Show Gist options
  • Select an option

  • Save JuliaKiselyova/0421cd90d822a91da97eff0d9a49d31d to your computer and use it in GitHub Desktop.

Select an option

Save JuliaKiselyova/0421cd90d822a91da97eff0d9a49d31d to your computer and use it in GitHub Desktop.
Get min max value to range filter from query. New callback is added to the filters https://prnt.sc/5TxhF-B5wYGj
<?php
add_filter('jet-smart-filters/filter-instance/args', function ($args) {
$filter_id = $args['filter_id'];
$query_var = get_post_meta($filter_id, '_query_var', true);
$step = get_post_meta($filter_id, '_source_step', true);
$source_cb = get_post_meta($filter_id, '_source_callback', true);
$source_query = get_post_meta($filter_id, '_source_query', true);
if (!$step) {
$step = 1;
}
if (is_callable($source_cb)) {
$data = call_user_func($source_cb, array('key' => $query_var . ',' . $source_query));
$min = isset($data['min']) ? $data['min'] : false;
$max = isset($data['max']) ? max_value_for_current_step($data['max'], $min, $step) : false;
$args['min'] = $min;
$args['max'] = $max;
}
return $args;
});
function max_value_for_current_step($max, $min, $step){
if ($step === 1) {
return $max;
}
$steps_count = ceil(($max - $min) / $step);
return $steps_count * $step + $min;
}
function custom_source_callback($args = array()){
if (!function_exists('jet_engine')) {
return $callbacks;
}
global $wpdb;
$key = !empty($args['key']) ? $args['key'] : false;
if (!$key) {
return array();
}
$key_parts = explode(',', $key);
if (count($key_parts) !== 2) {
return array();
}
$field = trim($key_parts[0]);
$query_id = trim($key_parts[1]);
// Use a macro to get post IDs
$macro_result = jet_engine()->listings->macros->do_macros('%query_results|' . $query_id . '|ids%');
if (is_string($macro_result)) {
$post_ids = explode(',', $macro_result);
} else {
$post_ids = $macro_result;
}
$post_id_condition = '';
if (!empty($post_ids)) {
$post_id_condition = " AND p.ID IN (" . implode(',', $post_ids) . ")";
}
$sql = "SELECT min(FLOOR(pm.meta_value)) as min, max(CEILING(pm.meta_value)) as max
FROM $wpdb->postmeta AS pm
INNER JOIN $wpdb->posts AS p ON p.ID = pm.post_id
WHERE pm.meta_key IN ('" . str_replace(',', '\',\'', str_replace(' ', '', $field)) . "')" . $post_id_condition;
$data = $wpdb->get_results($sql, ARRAY_A);
if (!empty($data)) {
return $data[0];
} else {
return array();
}
}
add_filter('jet-smart-filters/range/source-callbacks', function ($callbacks) {
if (!function_exists('jet_engine')) {
return $callbacks;
}
$callbacks['custom_source_callback'] = __('Get from JetEngine Query', 'jet-smart-filters');
return $callbacks;
});
@markoste
Copy link

Thank you for this function.

Just for info: I had to change:
$data = call_user_func($source_cb, array('key' => $query_var . ',' . $source_query));

into
$data = call_user_func( $source_cb, array( 'key' => $query_var ) );

to get it working. Otherwise $min and $max would always be false.

I also checked the database. The filter does not have a post_meta field _source_query.

Best regards
Stef

@JuliaKiselyova
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment