Created
December 8, 2023 13:17
-
-
Save ihslimn/57766f1e01b5cb46dc461f309fba829f to your computer and use it in GitHub Desktop.
get range min/max from query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'plugins_loaded', function() { | |
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 ) ) { | |
$source_cb = 'jet_smart_filters_meta_values'; | |
} | |
$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; | |
} ); | |
if ( ! function_exists( 'jet_engine' ) ) { | |
return; | |
} | |
add_filter('jet-smart-filters/post-type/meta-fields-settings', 'custom_register_controls_query'); | |
add_filter('jet-smart-filters/range/source-callbacks', function( $callbacks ) { | |
if ( ! function_exists( 'jet_engine' ) ) { | |
return $callbacks; | |
} | |
$callbacks['jec_get_values_from_query'] = __('Get from JetEngine Query', 'jet-smart-filters'); | |
return $callbacks; | |
}); | |
} ); | |
function custom_register_controls_query($fields) { | |
if ( ! function_exists( 'jet_engine' ) ) { | |
return; | |
} | |
$queries = \Jet_Engine\Query_Builder\Manager::instance()->get_queries_for_options(); | |
$insert = array( | |
'_source_query' => array( | |
'type' => 'select', | |
'title' => __('Select Query', 'jet-engine'), | |
'value' => '', | |
'options' => $queries, | |
'conditions' => array( | |
'_filter_type' => 'range', | |
'_source_callback' => 'jec_get_values_from_query', | |
), | |
), | |
); | |
$fields = jet_smart_filters()->utils->array_insert_after($fields, '_source_callback', $insert); | |
return $fields; | |
} | |
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 jec_get_values_from_query( $args = array() ){ | |
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment