Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Ordering Search Results › Reply To: Ordering Search Results
February 21, 2019 at 2:20 pm
#21214
Keymaster
Hi,
2. Well, the term order is not stored within the database, so there is only a ‘hacky’ way of doing it by using a custom code. The internal wordpress method does not work here, as that only works for the complete list of categories, but not partial results.
I have constructed a custom code, that may work in most cases, ordering the child categories below their parents. It’s definitely not the best solution, but it may do the trick.
Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!
add_filter('asp_results', 'asp_sort_terms_by_parent', 10, 1);
function asp_sort_terms_by_parent( $results ) {
$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
));
wd_sort_terms_hierarchicaly($terms, $sorted_terms);
wd_flatten_hierarchical_terms( $sorted_terms, $terms );
foreach ($terms as $k => $t) {
$final_terms[] = $res_terms[$t->term_id];
}
}
$results = array_merge($final_terms, $results);
return $results;
}