Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Passing a custom callback function and sorting based on the return. › Reply To: Passing a custom callback function and sorting based on the return.
October 7, 2020 at 9:52 am
#29666
Keymaster
Hi Gabe,
You are looking for the asp_results hook. That lets you make changes to the final results array before it is printed to the output.
In you case, I would start of with something like this:
add_filter( 'asp_results', 'asp_custom_results_order', 10, 1 );
function asp_custom_results_order( $results ) {
$front = array(); // Results prioritized
$back = array(); // Rest of the results
// Parse through each result item
foreach ($results as $k=>&$r) {
if ( isset($r->post_type) ) {
if ( your_function_to_check($r->id, get_current_user_id() ... ) ) {
$front[] = $r;
} else {
$back[] = $r;
}
} else {
$back[] = $r;
}
}
return array_merge($front, $back);
}
This code skeleton helps you separate the results into two parts – front and back. The $front array goes to the front of the list, the $back contains the rest of the results. Then you can go through all of the results, check whatever you need, and put the result into either the $front of the $back.