Created
January 9, 2024 10:12
-
-
Save JuliaKiselyova/6832a7ec743e2aa3c7299556cb0e7ebd 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/X7Ti9fMDl9zc
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 | |
| use Jet_Engine\Query_Builder\Manager; | |
| add_filter('jet-smart-filters/post-type/meta-fields-settings', 'custom_register_controls'); | |
| function custom_register_controls($fields) { | |
| $queries = 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' => 'custom_source_callback', | |
| ), | |
| ), | |
| ); | |
| $fields = jet_smart_filters()->utils->array_insert_after($fields, '_source_callback', $insert); | |
| return $fields; | |
| } |
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_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; | |
| } |
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 | |
| 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; | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment