This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Combine category and taxonomy

#20712
Ernest MarcinkoErnest Marcinko
Keymaster

Hi!

Well, this could get really difficult, as each taxonomy term is probably linking to a different location. I have put together small code snipptet, that will try to group the terms by name during the post process.
Try adding this custom code to the functions.php in your theme/child theme directory (copy from line 3 only!). Before editing, please make sure to have a full site back-up just in case!

add_filter('asp_results', 'asp_group_terms', 10, 1);
function asp_group_terms($results) {
  $terms = array();
  $terms_occurence = array();
  $new_results = array();
  
  foreach($results as $k=>&$r) {
    if ( isset($r->taxonomy) ) {
      preg_match('/^(.*?)\((.*?)\)/', $r->title, $m);
      if ( isset($m[1], $m[2]) ) {
        $terms[$m[1]] = isset( $terms[$m[1]] ) ? $terms[$m[1]] + $m[2] : $m[2];
      }
    }
  }
  foreach($results as $k=>&$r) {
    if ( isset($r->taxonomy) ) {
      preg_match('/^(.*?)\((.*?)\)/', $r->title, $m);
      if ( isset($m[1], $m[2]) ) {
        // Already occured once, reset it
        if ( !isset($terms_occurence[$m[1]]) ) {
          $r->title = $m[1] . '(' . $terms[$m[1]] . ')';
          $new_results[] = $r;
          $terms_occurence[$m[1]] = true;  
        }       
      } else {
        $new_results[] = $r;
      }
    } else {
      $new_results[] = $r;
    }
  }
  
  return $new_results;
}

Please note, that this might not work in each and every case though.