Last active
December 6, 2017 04:48
-
-
Save msaari/c1004e627d91f4a0f3b4371c75a2e5ee to your computer and use it in GitHub Desktop.
Relevanssi custom field filter
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 all three functions to theme functions.php, then call rlv_customfield_dropdown() on your search results template | |
| add_filter('relevanssi_hits_filter', 'rlv_gather_categories', 99); | |
| function rlv_gather_categories($hits) { | |
| global $rlv_categories_present; | |
| $rlv_categories_present = array(); | |
| $posts = array(); | |
| $today = date("Y-m-d"); | |
| foreach ( $hits[0] as $hit ) { | |
| $event_date = get_post_meta( $hit->ID, 'event_date', true ); | |
| if ($event_date < $today) continue; | |
| $posts[] = $hit; | |
| $terms = get_post_meta( $hit->ID, '_wmw_mbe_age' ); | |
| if (is_array($terms)) { | |
| foreach ( $terms as $term ) { | |
| $rlv_categories_present[$term] = true; | |
| } | |
| } | |
| } | |
| asort( array_keys($rlv_categories_present) ); | |
| $hits[0] = $posts; | |
| return $hits; | |
| } | |
| add_filter('relevanssi_modify_wp_query', 'rlv_custom_field_filter'); | |
| function rlv_custom_field_filter($query) { | |
| $tax_query = array('relation' => 'OR'); | |
| $cat_set = false; | |
| if (isset($_GET['cat1'])) { | |
| $tax_query[] = array( | |
| 'taxonomy' => 'category', | |
| 'terms' => $_GET['cat1'], | |
| 'field' => 'term_id', | |
| ); | |
| $cat_set = true; | |
| } | |
| if (isset($_GET['cat2'])) { | |
| $tax_query[] = array( | |
| 'taxonomy' => 'category', | |
| 'terms' => $_GET['cat2'], | |
| 'field' => 'term_id', | |
| ); | |
| $cat_set = true; | |
| } | |
| if ($cat_set) $query->set('tax_query', $tax_query); | |
| if (isset($_GET['value'])) { | |
| $meta_query = array( | |
| array( | |
| 'key' => 'total_sidebar_layout', | |
| 'value' => $_GET['value'], | |
| ), | |
| ); | |
| $query->set('meta_query', $meta_query); | |
| } | |
| return $query; | |
| } | |
| function rlv_category_dropdown() { | |
| global $rlv_categories_present, $wp_query; | |
| if (!empty($_GET['value'])) { | |
| $url = esc_url(remove_query_arg('value')); | |
| echo "<p><a href='$url'>Remove custom field filter</a>.</p>"; | |
| } | |
| else { | |
| $select = "<select id='rlv_meta' name='rlv_meta'><option value=''>Choose a value</option>"; | |
| foreach ( $rlv_categories_present as $cat_id => $cat_name ) { | |
| $select .= "<option>$cat_name</option>"; | |
| } | |
| $select .= "</select>"; | |
| $url = remove_query_arg('paged'); | |
| if (strpos($url, 'page') !== false) { | |
| $url = preg_replace('/page\/\d+\//', '', $url); | |
| } | |
| $select .= <<<EOH | |
| <button id="category_1" value="34">Toggle Cat 1</button> | |
| <button id="category_2" value="2">Toggle Cat 2</button> | |
| <script> | |
| <!-- | |
| var button_1 = document.getElementById("category_1"); | |
| function onButtonOne() { | |
| url = toggle("$url", "cat1", button_1.value); | |
| location.href = url; | |
| } | |
| button_1.onclick = onButtonOne; | |
| var button_2 = document.getElementById("category_2"); | |
| function onButtonTwo() { | |
| url = toggle("$url", "cat2", button_2.value); | |
| location.href = url; | |
| } | |
| button_2.onclick = onButtonTwo; | |
| var dropdown = document.getElementById("rlv_meta"); | |
| function onCatChange() { | |
| if ( dropdown.selectedIndex > 0 ) { | |
| location.href = "$url"+"&value="+dropdown.options[dropdown.selectedIndex].value; | |
| } | |
| } | |
| dropdown.onchange = onCatChange; | |
| function toggle(url, key, val) { | |
| var out = [], | |
| upd = '', | |
| rm = "([&?])" + key + "=([^&]*?,)?" + val + "(,.*?)?(&.*?)?$", | |
| ad = key + "=", | |
| rmrplr = function(url, p1, p2, p3, p4) { | |
| if (p2) { | |
| if (p3) out.push(p1, key, '=', p2, p3.substr(1)); | |
| else out.push(p1, key, '=', p2.substr(0, p2.length - 1)); | |
| } else { | |
| if (p3) out.push(p1, key, '=', p3.substr(1)); | |
| else out.push(p1); | |
| } | |
| if (p4) out.push(p4); | |
| return out.join('').replace(/([&?])&/, '$1').replace(/[&?]$/, ''); //<!2 | |
| }, | |
| adrplr = function(s) { | |
| return s + val + ','; | |
| }; | |
| if ((upd = url.replace(new RegExp(rm), rmrplr)) != url) return upd; | |
| if ((upd = url.replace(new RegExp(ad), adrplr)) != url) return upd; | |
| return url + (/\?.+/.test(url) ? '&' : '?') + key + '=' + val; //<!1 | |
| } | |
| --> | |
| </script> | |
| EOH; | |
| echo $select; | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment