Guest User

Untitled

a guest
Aug 11th, 2020
33
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*<!-- AUTHOR DROPDOWN --> */
  2. add_filter('relevanssi_hits_filter', 'rlv_gather_authors', 99);
  3. function rlv_gather_authors($hits) {
  4.     global $rlv_author_present;
  5.     $rlv_author_present = array();
  6.     foreach ( $hits[0] as $hit ) {
  7.         $terms = get_the_terms( $hit->ID, 'author' );
  8.         if (is_array($terms)) {
  9.             foreach ( $terms as $term ) {
  10.                 $rlv_author_present[ $term->term_id ] = $term->name;
  11.             }
  12.         }
  13.     }
  14.     asort( $rlv_author_present );
  15.     return $hits;
  16. }
  17. function rlv_author_dropdown() {
  18.     global $rlv_author_present, $wp_query;
  19.  
  20.     if (!empty($wp_query->query_vars['author'])) {
  21.         $url = esc_url(remove_query_arg('author'));
  22.         echo "<p><a href='$url'>Remove author filter</a>.</p>";
  23.     }
  24.     else {
  25.         $select = "<select class='select' id='rlv_author' name='rlv_author'><option value=''>Filter Results by Author</option>";
  26.         foreach ( $rlv_author_present as $author_id => $author_name ) {
  27.             $select .= "<option value='$author_id'>$author_name</option>";
  28.         }
  29.         $select .= "</select>";
  30.         $url = esc_url(remove_query_arg('paged'));
  31.         if (strpos($url, 'page') !== false) {
  32.             $url = preg_replace('/page\/\d+\//', '', $url);
  33.         }
  34.         $select .= <<<EOH
  35.         <script>
  36. <!--
  37.     var dropdown = document.getElementById("rlv_author");
  38.     function onAuthorChange() {
  39.         if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
  40.             location.href = "$url"+"&author="+dropdown.options[dropdown.selectedIndex].value;
  41.         }
  42.     }
  43.     dropdown.onchange = onAuthorChange;
  44. -->
  45. </script>
  46. EOH;
  47.  
  48.         echo $select;
  49.     }
  50. }
  51.  /*<!-- /AUTHOR DROPDOWN -->*/
RAW Paste Data