Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Ordering Search Results › Reply To: Ordering Search Results
March 12, 2019 at 9:53 am
#21576
Keymaster
Thank you very much. I may have found a possible solution, that may work in most cases, but to be honest I am not entirely sure. I conducted a few tests, and it seemed to work normally:
– Test 1: https://i.imgur.com/bteS8mb.png
– Test 2: https://i.imgur.com/ogioCKH.png
The final code is below. You can change the $sub_string variable on line 3, that is printed before each sub category.
add_filter('asp_results', 'asp_sort_terms_by_parent', 10, 1);
function asp_sort_terms_by_parent( $results ) {
$sub_string = '—'; // The string printed before each sub-level category
// --- DO NOT CHANGE ANYTHING BELOW ---
$term_ids = array();
$res_terms = array();
$sorted_terms = array();
$final_terms = array();
foreach ( $results as $k => $r ) {
if ( $r->content_type == 'term' ) {
$term_ids[] = $r->id;
$res_terms[$r->id] = $r;
unset($results[$k]);
}
}
if ( count($term_ids) > 0 ) {
$terms = get_terms(array(
'include' => $term_ids,
'hide_empty' => false
));
wd_sort_terms_hierarchicaly($terms, $sorted_terms);
wd_flatten_hierarchical_terms( $sorted_terms, $terms );
foreach ($terms as $k => $t) {
$tt = $res_terms[$t->term_id];
$tt->level = $t->level;
$final_terms[] = $tt;
}
foreach ($final_terms as &$fr) {
if ( $fr->level > 0 )
$fr->title = str_repeat($sub_string, $fr->level) . ' ' . $fr->title;
}
}
$results = array_merge($final_terms, $results);
return $results;
}