Thank you @tijmensmit and don’t worry about the delay – I appreciate you taking the time to come on here and help.
All of what you’ve said makes absolute sense however I don’t think I explained it very well.
I don’t want to toggle the style of featured stores. I’d like a dropdown box on the frontend that will show featured stores at the top of the results list by default and then the option of changing the results to be distance only.
So going off what you’ve said, inside my ajaxComplete function I’d need something like:
$(document).ajaxComplete(function () {
$("#dropdown").change(function () {
if($(this).val() == 'featured_first'){
// Put featured stores at the top
} else {
// Sort by distance (featured stores appear in normal distance order)
}
});
});
—
I can see that the function for sorting by featured first is as follows, so I just need a way of merging the two together somehow.
add_filter( 'wpsl_store_data', 'custom_store_data_sort' );
function custom_store_data_sort( $stores ) {
$premium_list = array();
foreach ( $stores as $k => $store ) {
// Check if the location is a premium one.
if ( isset( $store['featured'] ){
// Move the premium location to a new array
$premium_list[] = $store;
// Remove it from the existing $stores array
unset( $stores[$k] );
}
}
/**
* Merge the list of premium locations with the existing list.
* This will make sure the premium location show up before the normal locations.
*/
$results = array_merge( $premium_list, $stores );
return $results;
}
I hope this makes sense.